Main Questions
Our main question:
- How have global CO2 emission rates changed over time? In particular for the US, and how does the US compare to other countries?
- Are US CO2 emissions, global temperatures, and US storm rates associated?
Disclaimer: The purpose of the Open Case Studies project is to demonstrate the use of various data science methods, tools, and software in the context of messy, real-world data. A given case study does not cover all aspects of the research process, is not claiming to be the most appropriate way to analyze a given data set, and should not be used in the context of making policy decisions without external consultation from scientific experts.
This case study explores how different countries have contributed to Carbon Dioxide (CO2) emissions over time and how CO2 emission rates may relate to increasing global temperatures and increased rates of natural disasters and storms. This report provides a basis for the motivation: https://www.epa.gov/report-environment/greenhouse-gases.
CO2 makes up the largest proportion of greenhouse gas emissions in the United States:
A variety of sources and sectors contribute to greenhouse gas emissions, with transportation contributing the most metric tons of CO2:
So why should we pay attention to greenhouse gases?
According to the US Environmental Protection Agency (EPA) Inventory of U.S. Greenhouse Gas Emissions and Sinks 2020 Report:
Greenhouse gases absorb infrared radiation, thereby trapping heat in the atmosphere and making the planet warmer. The most important greenhouse gases directly emitted by humans include carbon dioxide (CO2), methane (CH4), nitrous oxide (N2O), and several fluorine-containing halogenated substances. Although CO2, CH4, and N2O occur naturally in the atmosphere, human activities have changed their atmospheric concentrations. From the pre- industrial era (i.e., ending about 1750) to 2018, concentrations of these greenhouse gases have increased globally by 46, 165, and 23 percent, respectively (IPCC 2013; NOAA/ESRL 2019a, 2019b, 2019c).
There are many signs that our planet is experiencing warmer temperatures:
The connection between greenhouse gas levels and global temperatures and the influence of increased global temperatures on human health are motivated by these reports:
Melillo, J.M., T.C. Richmond, and G.W. Yohe (eds.). 2014. Climate change impacts in the United States: The third National Climate Assessment. U.S. Global Change Research Program.
The National Climate Assessment Report states that:
Heat-trapping gases already in the atmosphere have committed us to a hotter future with more climate-related impacts over the next few decades. The magnitude of climate change beyond the next few decades depends primarily on the amount of heat-trapping gases that human activities emit globally, now and in the future.
Our main question:
In this case study, we will explore CO2 emission data from around the world. We will also focus on the US specifically to evaluate patterns of temperatures and storm activity. This case study will particularly focus on how to use different datasets that span different ranges of time, as well as how to create visualizations of patterns over time. We will especially focus on using packages and functions from the Tidyverse, such as dplyr, tidyr, plotlyand gganimate. The tidyverse is a library of packages created by RStudio. While some students may be familiar with previous R programming packages, these packages make data science in R especially efficient.
We will begin by loading the packages that we will need:
library(here)
library(readxl)
library(readr)
library(dplyr)
library(magrittr)
library(tidyverse)
library(plotly)
library(ggplot2)
library(gganimate)
library(ggrepel)
library(RColorBrewer)| Package | Use |
|---|---|
| here | to easily load and save data |
| readxl | to import the excel file data |
| readr | to import the csv file data |
| dplyr | to view and wrangle the data |
| magrittr | to use and reassign data objects using the %<>%pipe operator |
| tidyverse | to wrangle the data and create ggplot2 plots |
| plotyly | to make the visualizations |
| ggplot2 | to make visualizations |
| ggrepel | to add labels that don’t overlap to plots |
| gganimate | to make the plots interactive |
| RColorBrewer | to have greater control over the color in our plots |
The first time we use a function, we will use the :: to indicate which package we are using. Unless we have overlapping function names, this is not necessary, but we will include it here to be informative about where the functions we will use come from.
Greenhouse gas emissions are due to both natural processes and anthropogenic (human-derived) activities.
These emissions are one of the contributing factors to rising global temperatures, which can have a great influence on public health as illustrated in the following image:
Gases in the atmosphere can contribute to climate change both directly and indirectly. Direct effects occur when the gas itself absorbs radiation. Indirect radiative forcing occurs when chemical transformations of the substance produce other greenhouse gases, when a gas influences the atmospheric lifetimes of other gases, and/or when a gas affects atmospheric processes that alter the radiative balance of the earth (e.g., affect cloud formation or albedo). The IPCC developed the Global Warming Potential (GWP) concept to compare the ability of a greenhouse gas to trap heat in the atmosphere relative to another gas. The GWP of a greenhouse gas is defined as the ratio of the accumulated radiative forcing within a specific time horizon caused by emitting 1 kilogram of the gas, relative to that of the reference gas CO2 (IPCC 2013). Therefore GWP-weighted emissions are provided in million metric tons of CO2 equivalent (MMT CO2 Eq.)
CO2 is actually the least capable of the greenhouse gases for trapping heat:
However, because CO2 is so much more abundant and stays in the atmosphere so much longer than other greenhouse gases, it has been the largest contributor to global warming.
See here for more details.
Furthermore, sizing CO2 levels also influence ocean acidity:
This makes it difficult for organisms to maintain their shells or skeletons that are made of calcium carbonate, thus making it more difficult for these organisms to survive and impacting their role in the ecosystem and food chain.
Furthermore, greenhouse gas emissions are believed to influence storm rates.
Indeed events with high levels of precipitation which can induce flooding and property damage are generally increasing around the country:
There are some important considerations regarding this data analysis to keep in mind:
The datasets included only include countries and years in which countries were reporting such information to the agencies that collected the data. Thus the data is incomplete. For example while we have a fairly good sense of CO2 emissions globally for later years, additional emissions were also produced by countries that are not included in the data.
Correlation or association does not imply causation. We will be showing how different datasets show similar trends across time. This does not imply that one caused the other. However, in the case of some of the data we will show, there is additional scientific evidence to suggest that for example, increased CO2 emissions may cause increased temperatures or increased rates of disastors. However, simply showing a similar trend over time does not in itself prove that two variables are causally related. As you can see from this plot, often data may show a similar pattern over time by random chance. See this website for more examples.
In this case study we will be using data related to CO2 emissions, as well as other data that may influence, be influenced or relate to CO2 emissions. Most of our data was obtained from Gapminder, which is a unique nonprofit that provides a variety of data for free.
In their words, Gapminder is…
Gapminder is an independent Swedish foundation with no political, religious or economic affiliations. Gapminder is a fact tank, not a think tank. Gapminder fights devastating misconceptions about global development. Gapminder produces free teaching resources making the world understandable based on reliable statistics. Gapminder promotes a fact-based worldview everyone can understand. Gapminder collaborates with universities, UN, public agencies and non-governmental organizations. All Gapminder activities are governed by the board. We do not award grants. Gapminder Foundation is registered at Stockholm County Administration Board. Our constitution can be found here.
The data that we will be using from Gapminder was obtained from the World Bank.
In addition we will use some data that is specific to the United States from the [National Oceanic and Atmospheric Administration (NOAA)] (https://www.noaa.gov/), which is an agency that collects weather and climate data.
| Data | Time span | Source | Orginal Source | Description | Citation |
|---|---|---|---|---|---|
| CO2 emissions | 1751 to 2014 | Gapminder | Carbon Dioxid Information Analysis Center (CDIAC) | CO2 emissions in tonnes or metric tons (equivalent to approximately 2,204.6 pounds) per person by country | NA |
| GDP per capita, percent yearly growth | 1801 to 2019 | Gapminder | World Bank | Growth Domestic Product (which is an overall measure of the health of nation’s economy) per person by country | NA |
| Energy use per person | 1960 to 2015 | Gapminder | World Bank | Use of primary energy before transformation to other end-use fules, by country | NA |
| Crude Mortality Rate | 1960 to 2018 | World Bank | World Bank | Death rate per 1,000 people by country | NA |
| US Natural Disasters | 1980 to 2019 | The National Oceanic and Atmospheric Administration (NOAA) | The National Oceanic and Atmospheric Administration (NOAA) | US data about: – Droughts – Floods – Freezes – Severe Storms – Tropical Cyclones – Wildfires – Winter Storms |
NOAA National Centers for Environmental Information (NCEI) U.S. Billion-Dollar Weather and Climate Disasters (2020). https://www.ncdc.noaa.gov/billions/, DOI: 10.25921/stkw-7w73 |
| Temperature | 1895 to 2019 | The National Oceanic and Atmospheric Administration (NOAA) | The National Oceanic and Atmospheric Administration (NOAA) | US National yearly average temperature (in Fahrenheit) from 1895 to 2019 | NOAA National Centers for Environmental information, Climate at a Glance: National Time Series, published June 2020, retrieved on June 26, 2020 from https://www.ncdc.noaa.gov/cag/ |
To obtain the temperature data, annual average temperatures were selected as shown in this image:
Importantly, notice that the data we would like to use span different time periods:
| Data | Time span |
|---|---|
| CO2 emissions | 1751 to 2014 |
| GDP per capita, yearly growth | 1801 to 2019 |
| Energy use per person | 1960 to 2015 |
| Crude Mortality Rate | 1960 to 2018 |
| US Natural Disasters | 1980 to 2019 |
| Temperature | 1895 to 2019 |
To read in the files that were downloaded from the various sources as indicated in the table above, we will use the read_xlsx() and read_xls() functions of the readxl package to import the data from the .xlsx and .xls files respectively and we will use the read_csv function of the readr package to import the data from the csv files.
# xlsx files:
CO2_emissions <- readxl::read_xlsx(here("docs/yearly_co2_emissions_1000_tonnes.xlsx"))
gdp_growth <- readxl::read_xlsx(here("docs/gdp_per_capita_yearly_growth.xlsx"))
energy_use <- readxl::read_xlsx(here("docs/energy_use_per_person.xlsx"))
# xls file:
mortality <- readxl::read_xls(here("docs/API_SP.DYN.CDRT.IN_DS2_en_excel_v2_804384.xls"))For our csv data files, there are some lines that we would like to not import - infact, we will get an error if we try to import them because our table structure will be as r expects. We can do so using the skip = argument of the read_csv() function.
Here you can see that the first two rows of the data about US Disasters doesn’t have the same number of columns as the subsequent rows. So we want to skip these first two lines, we will use skip = 2 for this.
Now looking at the temperature data, we can see that the first four lines do not have the same number of columns as the subsequent lines. We will skip importing all 4 lines by using
skip = 4. We can also specify that NA values are encoded as "-99". This will replace all instances of "-99" with NA. We can do this using the na = argument of the read_csv() function. We will do so as: na = "-99". The “-99” needs to be in quotation markes becuase this argument expects characters.
#csv files:
us_disaster <- readr::read_csv(here("docs/time-series-US.csv"), skip = 2)
us_temperature <- readr::read_csv(here("docs/temperature.csv"), skip = 4, na ="-99")
mortality2 <-readr::read_csv(here("docs/mortality.csv"), skip = 5)Great! now we have imported all of the data that we will need.
Now we will take a look at our data and wrangle it until it is easy to use to allow us to evaluate how CO2 emissions have changed over time and how emissions may relate to energy use, mortality, GDP etc.
First let’s take a look at the CO2 data. We can use the base head() function to see just the first 6 rows of our data.
We will use the %>% pipe which can be used to define the input for later sequential steps. This will make more sense when we have multiple sequential steps using the same data object. To use the pipe notation we need to install and load the dplyr package.
# A tibble: 6 x 265
country `1751` `1752` `1753` `1754` `1755` `1756` `1757` `1758` `1759` `1760`
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Afghan… NA NA NA NA NA NA NA NA NA NA
2 Albania NA NA NA NA NA NA NA NA NA NA
3 Algeria NA NA NA NA NA NA NA NA NA NA
4 Andorra NA NA NA NA NA NA NA NA NA NA
5 Angola NA NA NA NA NA NA NA NA NA NA
6 Antigu… NA NA NA NA NA NA NA NA NA NA
# … with 254 more variables: `1761` <dbl>, `1762` <dbl>, `1763` <dbl>,
# `1764` <dbl>, `1765` <dbl>, `1766` <dbl>, `1767` <dbl>, `1768` <dbl>,
# `1769` <dbl>, `1770` <dbl>, `1771` <dbl>, `1772` <dbl>, `1773` <dbl>,
# `1774` <dbl>, `1775` <dbl>, `1776` <dbl>, `1777` <dbl>, `1778` <dbl>,
# `1779` <dbl>, `1780` <dbl>, `1781` <dbl>, `1782` <dbl>, `1783` <dbl>,
# `1784` <dbl>, `1785` <dbl>, `1786` <dbl>, `1787` <dbl>, `1788` <dbl>,
# `1789` <dbl>, `1790` <dbl>, `1791` <dbl>, `1792` <dbl>, `1793` <dbl>,
# `1794` <dbl>, `1795` <dbl>, `1796` <dbl>, `1797` <dbl>, `1798` <dbl>,
# `1799` <dbl>, `1800` <dbl>, `1801` <dbl>, `1802` <dbl>, `1803` <dbl>,
# `1804` <dbl>, `1805` <dbl>, `1806` <dbl>, `1807` <dbl>, `1808` <dbl>,
# `1809` <dbl>, `1810` <dbl>, `1811` <dbl>, `1812` <dbl>, `1813` <dbl>,
# `1814` <dbl>, `1815` <dbl>, `1816` <dbl>, `1817` <dbl>, `1818` <dbl>,
# `1819` <dbl>, `1820` <dbl>, `1821` <dbl>, `1822` <dbl>, `1823` <dbl>,
# `1824` <dbl>, `1825` <dbl>, `1826` <dbl>, `1827` <dbl>, `1828` <dbl>,
# `1829` <dbl>, `1830` <dbl>, `1831` <dbl>, `1832` <dbl>, `1833` <dbl>,
# `1834` <dbl>, `1835` <dbl>, `1836` <dbl>, `1837` <dbl>, `1838` <dbl>,
# `1839` <dbl>, `1840` <dbl>, `1841` <dbl>, `1842` <dbl>, `1843` <dbl>,
# `1844` <dbl>, `1845` <dbl>, `1846` <dbl>, `1847` <dbl>, `1848` <dbl>,
# `1849` <dbl>, `1850` <dbl>, `1851` <dbl>, `1852` <dbl>, `1853` <dbl>,
# `1854` <dbl>, `1855` <dbl>, `1856` <dbl>, `1857` <dbl>, `1858` <dbl>,
# `1859` <dbl>, `1860` <dbl>, …
OK, we can see that our country data makes of the rows and the yearly data makes up the columns. We also see that we have alot of NA values.
We can also use the glimpse() function of the dplyr packge to view our data. This allows us to see more of our data at once. We will see a tiny bit of each variable/column. To do so our data will be displayed with the column names listed on the right.
Rows: 192
Columns: 265
$ country <chr> "Afghanistan", "Albania", "Algeria", "Andorra", "Angola", "An…
$ `1751` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1752` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1753` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1754` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1755` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1756` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1757` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1758` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1759` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1760` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1761` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1762` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1763` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1764` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1765` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1766` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1767` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1768` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1769` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1770` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1771` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1772` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1773` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1774` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1775` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1776` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1777` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1778` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1779` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1780` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1781` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1782` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1783` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1784` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1785` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1786` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1787` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1788` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1789` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1790` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1791` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1792` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1793` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1794` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1795` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1796` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1797` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1798` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1799` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1800` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1801` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1802` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1803` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1804` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1805` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1806` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1807` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 169, NA, NA, NA, NA, NA, …
$ `1808` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1809` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1810` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1811` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1812` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1813` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1814` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1815` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1816` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1817` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1818` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ `1819` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 253, NA, NA, NA, NA, NA, …
$ `1820` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 334, NA, NA, NA, NA, NA, …
$ `1821` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 359, NA, NA, NA, NA, NA, …
$ `1822` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 367, NA, NA, NA, NA, NA, …
$ `1823` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 348, NA, NA, NA, NA, NA, …
$ `1824` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 400, NA, NA, NA, NA, NA, …
$ `1825` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 403, NA, NA, NA, NA, NA, …
$ `1826` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 458, NA, NA, NA, NA, NA, …
$ `1827` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 477, NA, NA, NA, NA, NA, …
$ `1828` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 458, NA, NA, NA, NA, NA, …
$ `1829` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 477, NA, NA, NA, NA, NA, …
$ `1830` <dbl> NA, NA, NA, NA, NA, NA, NA, 0.032, NA, 495.000, 0.308, NA, NA…
$ `1831` <dbl> NA, NA, NA, NA, NA, NA, NA, 3.84e-02, NA, 4.80e+02, 3.70e-01,…
$ `1832` <dbl> NA, NA, NA, NA, NA, NA, NA, 2.56e-02, NA, 5.13e+02, 2.47e-01,…
$ `1833` <dbl> NA, NA, NA, NA, NA, NA, NA, 0.032, NA, 429.000, 0.308, NA, NA…
$ `1834` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 587, NA, NA, NA, NA, NA, …
$ `1835` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 634, NA, NA, NA, NA, NA, …
$ `1836` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 675, NA, NA, NA, NA, NA, …
$ `1837` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 708, NA, NA, NA, NA, NA, …
$ `1838` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 851, NA, NA, NA, NA, NA, …
$ `1839` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 1060, NA, NA, NA, NA, NA,…
$ `1840` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 1170, NA, NA, NA, NA, NA,…
$ `1841` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 1320, NA, NA, NA, NA, NA,…
$ `1842` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 1460, NA, NA, NA, NA, NA,…
$ `1843` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 1270, NA, NA, NA, NA, NA,…
$ `1844` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 1600, NA, NA, NA, NA, NA,…
$ `1845` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 1800, NA, NA, NA, NA, NA,…
$ `1846` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 2120, NA, NA, NA, NA, NA,…
$ `1847` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 2080, NA, NA, NA, NA, NA,…
$ `1848` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 2340, NA, NA, NA, NA, NA,…
$ `1849` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 2260, NA, NA, NA, NA, NA,…
$ `1850` <dbl> NA, NA, NA, NA, NA, NA, NA, 0.198, NA, 2330.000, 1.910, NA, N…
$ `1851` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 2340, NA, NA, NA, NA, NA,…
$ `1852` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 2810, NA, NA, NA, NA, NA,…
$ `1853` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 3230, NA, NA, NA, NA, NA,…
$ `1854` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 3180, NA, NA, NA, NA, NA,…
$ `1855` <dbl> NA, NA, NA, NA, NA, NA, NA, 6.01e-01, NA, 3.70e+03, 5.80e+00,…
$ `1856` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 4240, NA, NA, NA, NA, NA,…
$ `1857` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 4880, NA, NA, NA, NA, NA,…
$ `1858` <dbl> NA, NA, NA, NA, NA, NA, NA, 8.44e-01, NA, 7.25e+03, 8.14e+00,…
$ `1859` <dbl> NA, NA, NA, NA, NA, NA, NA, 8.95e-01, NA, 5.87e+03, 8.64e+00,…
$ `1860` <dbl> NA, NA, NA, NA, NA, NA, NA, 1.18, 279.00, 6150.00, 11.40, NA,…
$ `1861` <dbl> NA, NA, NA, NA, NA, NA, NA, 1.5, 510.0, 6380.0, 14.5, NA, NA,…
$ `1862` <dbl> NA, NA, NA, NA, NA, NA, NA, 1.36, 356.00, 6360.00, 13.10, NA,…
$ `1863` <dbl> NA, NA, NA, NA, NA, NA, NA, 1.42, 400.00, 5880.00, 13.70, NA,…
$ `1864` <dbl> NA, NA, NA, NA, NA, NA, NA, 1.59, 268.00, 5080.00, 15.40, NA,…
$ `1865` <dbl> NA, NA, NA, NA, NA, NA, NA, 1.52, 422.00, 5360.00, 14.70, NA,…
$ `1866` <dbl> NA, NA, NA, NA, NA, NA, NA, 4.81, 697.00, 3600.00, 46.40, NA,…
$ `1867` <dbl> NA, NA, NA, NA, NA, NA, NA, 5.52, 895.00, 4920.00, 53.20, NA,…
$ `1868` <dbl> NA, NA, NA, NA, NA, NA, NA, 4.59, 733.00, 6080.00, 44.30, NA,…
$ `1869` <dbl> NA, NA, NA, NA, NA, NA, NA, 6.23, 642.00, 6490.00, 60.10, NA,…
$ `1870` <dbl> NA, NA, NA, NA, NA, NA, NA, 6.76, 601.00, 7370.00, 65.20, NA,…
$ `1871` <dbl> NA, NA, NA, NA, NA, NA, NA, 9.12, 693.00, 10200.00, 88.00, NA…
$ `1872` <dbl> NA, NA, NA, NA, NA, NA, NA, 9.36, 708.00, 10000.00, 90.40, NA…
$ `1873` <dbl> NA, NA, NA, NA, NA, NA, NA, 8.79, 869.00, 10700.00, 84.80, NA…
$ `1874` <dbl> NA, NA, NA, NA, NA, NA, NA, 10.7, 891.0, 9160.0, 103.0, NA, N…
$ `1875` <dbl> NA, NA, NA, NA, NA, NA, NA, 12.3, 829.0, 7870.0, 119.0, NA, N…
$ `1876` <dbl> NA, NA, NA, NA, NA, NA, NA, 15.2, 931.0, 8100.0, 147.0, NA, N…
$ `1877` <dbl> NA, NA, NA, NA, NA, NA, NA, 15.6, 1070.0, 7290.0, 150.0, NA, …
$ `1878` <dbl> NA, NA, NA, NA, NA, NA, NA, 20.3, 968.0, 7250.0, 196.0, NA, N…
$ `1879` <dbl> NA, NA, NA, NA, NA, NA, NA, 20.9, 1460.0, 8870.0, 201.0, NA, …
$ `1880` <dbl> NA, NA, NA, NA, NA, NA, NA, 24.5, 2210.0, 23700.0, 236.0, NA,…
$ `1881` <dbl> NA, NA, NA, NA, NA, NA, NA, 25.80, 1770.00, 10300.00, 249.00,…
$ `1882` <dbl> NA, NA, NA, NA, NA, NA, NA, 27.20, 2010.00, 10600.00, 262.00,…
$ `1883` <dbl> NA, NA, NA, NA, NA, NA, NA, 30.90, 2430.00, 11800.00, 298.00,…
$ `1884` <dbl> NA, NA, NA, NA, NA, NA, NA, 31.4, 2570.0, 11500.0, 303.0, NA,…
$ `1885` <dbl> NA, NA, NA, NA, NA, NA, NA, 34.20, 2910.00, 12100.00, 330.00,…
$ `1886` <dbl> NA, NA, NA, NA, NA, NA, NA, 35.10, 2890.00, 11400.00, 338.00,…
$ `1887` <dbl> NA, NA, NA, NA, NA, NA, 1090.0, 37.1, 3040.0, 12300.0, 358.0,…
$ `1888` <dbl> NA, NA, NA, NA, NA, NA, 891.0, 38.7, 3530.0, 12000.0, 373.0, …
$ `1889` <dbl> NA, NA, NA, NA, NA, NA, 1760.0, 41.8, 3430.0, 12900.0, 403.0,…
$ `1890` <dbl> NA, NA, NA, NA, NA, NA, 1370.0, 47.3, 3550.0, 13000.0, 457.0,…
$ `1891` <dbl> NA, NA, NA, NA, NA, NA, 939.0, 52.1, 4010.0, 15000.0, 503.0, …
$ `1892` <dbl> NA, NA, NA, NA, NA, NA, 1390.0, 55.1, 4150.0, 14500.0, 532.0,…
$ `1893` <dbl> NA, NA, NA, NA, NA, NA, 1550.0, 64.6, 3970.0, 17700.0, 624.0,…
$ `1894` <dbl> NA, NA, NA, NA, NA, NA, 1990.0, 65.8, 4360.0, 18100.0, 635.0,…
$ `1895` <dbl> NA, NA, NA, NA, NA, NA, 2270.0, 75.6, 4590.0, 20400.0, 730.0,…
$ `1896` <dbl> NA, NA, NA, NA, NA, NA, 2310, 77, 4510, 21300, 743, NA, NA, N…
$ `1897` <dbl> NA, NA, NA, NA, NA, NA, 2080, 89, 4980, 23000, 859, NA, NA, N…
$ `1898` <dbl> NA, NA, NA, NA, NA, NA, 2350.0, 99.9, 5620.0, 24500.0, 964.0,…
$ `1899` <dbl> NA, NA, NA, NA, NA, NA, 2920, 116, 5790, 24800, 1120, NA, NA,…
$ `1900` <dbl> NA, NA, NA, NA, NA, NA, 2070, 131, 10200, 27700, 1270, NA, NA…
$ `1901` <dbl> NA, NA, NA, NA, NA, NA, 2490, 135, 11400, 28400, 1300, NA, NA…
$ `1902` <dbl> NA, NA, NA, NA, NA, NA, 2820, 130, 11400, 25700, 1260, NA, NA…
$ `1903` <dbl> NA, NA, NA, NA, NA, NA, 2860, 127, 11200, 25600, 1230, NA, NA…
$ `1904` <dbl> NA, NA, NA, NA, NA, NA, 3800, 142, 11600, 26900, 1370, NA, NA…
$ `1905` <dbl> NA, NA, NA, NA, NA, NA, 3990, 126, 12100, 28100, 1220, NA, NA…
$ `1906` <dbl> NA, NA, NA, NA, NA, NA, 6260, 144, 14400, 33600, 1390, NA, NA…
$ `1907` <dbl> NA, NA, NA, NA, NA, NA, 6260, 161, 15500, 42200, 1560, NA, NA…
$ `1908` <dbl> NA, NA, NA, NA, NA, NA, 7620, 162, 16800, 59000, 1570, NA, NA…
$ `1909` <dbl> NA, NA, NA, NA, NA, NA, 5940, 172, 14600, 42200, 1660, NA, NA…
$ `1910` <dbl> NA, NA, NA, NA, NA, NA, 8910, 168, 17500, 57600, 1620, NA, NA…
$ `1911` <dbl> NA, NA, NA, NA, NA, NA, 9950, 174, 19300, 48100, 1680, NA, NA…
$ `1912` <dbl> NA, NA, NA, NA, NA, NA, 9490, 198, 20800, 50000, 1910, NA, NA…
$ `1913` <dbl> NA, NA, NA, NA, NA, NA, 10200, 215, 22400, 59700, 2070, NA, N…
$ `1914` <dbl> NA, NA, NA, NA, NA, NA, 8680, 194, 24500, 48900, 1870, NA, NA…
$ `1915` <dbl> NA, NA, NA, NA, NA, NA, 6950, 178, 21800, 34900, 1720, NA, NA…
$ `1916` <dbl> NA, NA, 3.67, NA, NA, NA, 4990.00, 189.00, 19300.00, 8040.00,…
$ `1917` <dbl> NA, NA, 7.33, NA, NA, NA, 2230.00, 174.00, 20800.00, 3450.00,…
$ `1918` <dbl> NA, NA, 18.3, NA, NA, NA, 2520.0, 69.5, 23000.0, 3340.0, 671.…
$ `1919` <dbl> NA, NA, 18.3, NA, NA, NA, 3730.0, 59.4, 21800.0, 3020.0, 573.…
$ `1920` <dbl> NA, NA, 22.0, NA, NA, NA, 5900.0, 54.2, 25800.0, 14500.0, 523…
$ `1921` <dbl> NA, NA, 25.7, NA, NA, NA, 5540.0, 58.7, 23200.0, 19400.0, 567…
$ `1922` <dbl> NA, NA, 25.7, NA, NA, NA, 7300.0, 71.6, 24400.0, 18600.0, 692…
$ `1923` <dbl> NA, NA, 14.7, NA, NA, NA, 8450.0, 79.1, 24900.0, 17800.0, 764…
$ `1924` <dbl> NA, NA, 29.3, NA, NA, NA, 11000.0, 94.3, 27100.0, 20100.0, 91…
$ `1925` <dbl> NA, NA, 33.0, NA, NA, NA, 11200.0, 93.1, 28300.0, 19000.0, 89…
$ `1926` <dbl> NA, NA, 40.3, NA, NA, NA, 11300.0, 135.0, 27900.0, 18600.0, 1…
$ `1927` <dbl> NA, NA, 58.7, NA, NA, NA, 13400.0, 168.0, 28900.0, 20100.0, 1…
$ `1928` <dbl> NA, NA, 73.30, NA, NA, NA, 12800.00, 186.00, 26300.00, 21200.…
$ `1929` <dbl> NA, NA, 80.70, NA, NA, NA, 13100.00, 201.00, 23700.00, 24200.…
$ `1930` <dbl> NA, NA, 84.30, NA, NA, NA, 12800.00, 273.00, 22000.00, 18900.…
$ `1931` <dbl> NA, NA, 99.00, NA, NA, NA, 12900.00, 328.00, 19600.00, 18100.…
$ `1932` <dbl> NA, NA, 114.00, NA, NA, NA, 13100.00, 369.00, 20400.00, 15200…
$ `1933` <dbl> NA, 7.33, 121.00, NA, NA, NA, 13200.00, 412.00, 21600.00, 142…
$ `1934` <dbl> NA, 7.33, 139.00, NA, NA, NA, 14300.00, 499.00, 22700.00, 138…
$ `1935` <dbl> NA, 18.3, 132.0, NA, NA, NA, 14000.0, 565.0, 25300.0, 13900.0…
$ `1936` <dbl> NA, 128.0, 51.3, NA, NA, NA, 15100.0, 648.0, 27100.0, 13600.0…
$ `1937` <dbl> NA, 297.0, 69.7, NA, NA, NA, 16700.0, 662.0, 28900.0, 15300.0…
$ `1938` <dbl> NA, 348, 33, NA, NA, NA, 16400, 699, 28100, 5790, 6750, NA, 3…
$ `1939` <dbl> NA, 433.00, 161.00, NA, NA, NA, 17400.00, 707.00, 32200.00, 6…
$ `1940` <dbl> NA, 693, 238, NA, NA, NA, 15900, 848, 29100, 7350, 8190, NA, …
$ `1941` <dbl> NA, 627, 312, NA, NA, NA, 14000, 745, 34600, 7980, 7190, NA, …
$ `1942` <dbl> NA, 744, 499, NA, NA, NA, 13500, 513, 36500, 8560, 4950, NA, …
$ `1943` <dbl> NA, 462, 469, NA, NA, NA, 14100, 655, 35000, 9620, 6320, NA, …
$ `1944` <dbl> NA, 154, 499, NA, NA, NA, 14000, 613, 34200, 9400, 5920, NA, …
$ `1945` <dbl> NA, 121, 616, NA, NA, NA, 13700, 649, 32700, 4570, 6270, NA, …
$ `1946` <dbl> NA, 484, 763, NA, NA, NA, 13700, 730, 35500, 12800, 7040, NA,…
$ `1947` <dbl> NA, 928.00, 744.00, NA, NA, NA, 14500.00, 878.00, 38000.00, 1…
$ `1948` <dbl> NA, 704.00, 803.00, NA, NA, NA, 17400.00, 935.00, 38500.00, 2…
$ `1949` <dbl> 14.70, 1020.00, 909.00, NA, NA, NA, 15400.00, 1060.00, 37700.…
$ `1950` <dbl> 84.3, 297.0, 3790.0, NA, 187.0, NA, 30000.0, 1180.0, 54800.0,…
$ `1951` <dbl> 91.7, 403.0, 4140.0, NA, 249.0, NA, 35000.0, 1280.0, 59100.0,…
$ `1952` <dbl> 91.7, 374.0, 3890.0, NA, 312.0, NA, 36100.0, 1370.0, 60300.0,…
$ `1953` <dbl> 106.0, 414.0, 4000.0, NA, 275.0, NA, 35200.0, 1450.0, 59500.0…
$ `1954` <dbl> 106.0, 502.0, 4160.0, NA, 348.0, NA, 36800.0, 1590.0, 67900.0…
$ `1955` <dbl> 154.0, 664.0, 4610.0, NA, 414.0, NA, 39600.0, 1800.0, 70700.0…
$ `1956` <dbl> 183.0, 840.0, 5000.0, NA, 502.0, NA, 44300.0, 1970.0, 73100.0…
$ `1957` <dbl> 293.0, 1510.0, 5540.0, NA, 620.0, 22.0, 47700.0, 2160.0, 7460…
$ `1958` <dbl> 330.0, 1200.0, 5220.0, NA, 594.0, 29.3, 44200.0, 2310.0, 7770…
$ `1959` <dbl> 385.0, 1440.0, 5670.0, NA, 620.0, 29.3, 49000.0, 2430.0, 8380…
$ `1960` <dbl> 414.0, 2020.0, 6160.0, NA, 550.0, 36.7, 48800.0, 2530.0, 8820…
$ `1961` <dbl> 491.0, 2280.0, 6070.0, NA, 455.0, 47.7, 51200.0, 2600.0, 9060…
$ `1962` <dbl> 689.0, 2460.0, 5670.0, NA, 1180.0, 103.0, 53700.0, 2730.0, 94…
$ `1963` <dbl> 708.0, 2080.0, 5430.0, NA, 1150.0, 84.3, 50100.0, 2930.0, 101…
$ `1964` <dbl> 840.0, 2020.0, 5650.0, NA, 1220.0, 91.7, 55700.0, 3120.0, 109…
$ `1965` <dbl> 1010.0, 2170.0, 6600.0, NA, 1190.0, 150.0, 58900.0, 3310.0, 1…
$ `1966` <dbl> 1090.0, 2550.0, 8430.0, NA, 1550.0, 348.0, 63100.0, 3490.0, 1…
$ `1967` <dbl> 1280, 2680, 8440, NA, 994, 565, 65500, 3650, 129000, 40000, 3…
$ `1968` <dbl> 1220, 3070, 9060, NA, 1670, 990, 69100, 3750, 135000, 42400, …
$ `1969` <dbl> 942, 3250, 11300, NA, 2790, 1260, 77300, 3910, 142000, 44700,…
$ `1970` <dbl> 1.67e+03, 3.74e+03, 1.51e+04, NA, 3.58e+03, 4.62e+02, 8.27e+0…
$ `1971` <dbl> 1.90e+03, 4.35e+03, 1.87e+04, NA, 3.41e+03, 4.25e+02, 8.89e+0…
$ `1972` <dbl> 1.53e+03, 5.64e+03, 2.83e+04, NA, 4.51e+03, 3.74e+02, 9.02e+0…
$ `1973` <dbl> 1.64e+03, 5.29e+03, 3.83e+04, NA, 4.88e+03, 3.30e+02, 9.41e+0…
$ `1974` <dbl> 1.92e+03, 4.35e+03, 3.19e+04, NA, 4.87e+03, 4.29e+02, 9.56e+0…
$ `1975` <dbl> 2.13e+03, 4.59e+03, 3.20e+04, NA, 4.42e+03, 7.08e+02, 9.49e+0…
$ `1976` <dbl> 1.99e+03, 4.95e+03, 3.92e+04, NA, 3.29e+03, 4.03e+02, 9.98e+0…
$ `1977` <dbl> 2.39e+03, 5.72e+03, 4.19e+04, NA, 3.53e+03, 4.66e+02, 1.01e+0…
$ `1978` <dbl> 2160, 6490, 62500, NA, 5410, 491, 103000, 5810, 202000, 57500…
$ `1979` <dbl> 2240, 7590, 45600, NA, 5500, 407, 111000, 5850, 205000, 61600…
$ `1980` <dbl> 1760, 5170, 66500, NA, 5350, 143, 109000, 6080, 221000, 52300…
$ `1981` <dbl> 1980.0, 7340.0, 46400.0, NA, 5280.0, 106.0, 102000.0, 5970.0,…
$ `1982` <dbl> 2100, 7310, 39300, NA, 4650, 293, 103000, 6080, 234000, 53900…
$ `1983` <dbl> 2520.0, 7630.0, 52600.0, NA, 5120.0, 84.3, 105000.0, 6170.0, …
$ `1984` <dbl> 2830.0, 7830.0, 71100.0, NA, 5010.0, 147.0, 107000.0, 6230.0,…
$ `1985` <dbl> 3510.0, 7880.0, 72800.0, NA, 4700.0, 249.0, 101000.0, 6710.0,…
$ `1986` <dbl> 3140, 8060, 76300, NA, 4660, 249, 104000, 6730, 240000, 54100…
$ `1987` <dbl> 3120, 7440, 84100, NA, 5820, 275, 115000, 7020, 256000, 57700…
$ `1988` <dbl> 2870, 7330, 83900, NA, 5130, 286, 121000, 7210, 261000, 53300…
$ `1989` <dbl> 2780.0, 8980.0, 80000.0, NA, 5010.0, 286.0, 117000.0, 7060.0,…
$ `1990` <dbl> 2610, 5520, 77000, 407, 5120, 282, 112000, 6620, 264000, 5770…
$ `1991` <dbl> 2440, 4290, 79000, 407, 5090, 268, 117000, 6380, 261000, 6160…
$ `1992` <dbl> 1390, 2520, 80100, 407, 5200, 264, 121000, 5830, 268000, 5670…
$ `1993` <dbl> 1350, 2340, 82200, 411, 5780, 271, 118000, 2560, 277000, 5710…
$ `1994` <dbl> 1290, 1930, 86400, 407, 3890, 268, 122000, 2710, 278000, 5710…
$ `1995` <dbl> 1240, 2090, 95300, 425, 11000, 275, 128000, 3410, 282000, 598…
$ `1996` <dbl> 1180, 2020, 97100, 455, 10500, 293, 135000, 2560, 302000, 632…
$ `1997` <dbl> 1100, 1540, 87300, 466, 7380, 308, 138000, 3230, 306000, 6270…
$ `1998` <dbl> 1040, 1750, 107000, 491, 7310, 319, 140000, 3360, 317000, 637…
$ `1999` <dbl> 821, 2980, 92000, 513, 9160, 330, 147000, 3010, 325000, 61900…
$ `2000` <dbl> 774, 3020, 87900, 524, 9540, 345, 142000, 3470, 329000, 62300…
$ `2001` <dbl> 818, 3220, 84200, 524, 9730, 348, 134000, 3540, 325000, 65900…
$ `2002` <dbl> 1070, 3750, 89900, 532, 12700, 370, 125000, 3040, 341000, 671…
$ `2003` <dbl> 1200, 4290, 91600, 535, 9060, 403, 135000, 3430, 336000, 7220…
$ `2004` <dbl> 950, 4170, 88500, 561, 18800, 422, 158000, 3640, 343000, 7240…
$ `2005` <dbl> 1330, 4250, 107000, 576, 19200, 429, 162000, 4350, 350000, 74…
$ `2006` <dbl> 1650, 3900, 101000, 546, 22300, 444, 175000, 4380, 365000, 72…
$ `2007` <dbl> 2270, 3930, 109000, 539, 25200, 469, 175000, 5060, 372000, 69…
$ `2008` <dbl> 4210, 4370, 110000, 539, 25700, 480, 189000, 5560, 386000, 69…
$ `2009` <dbl> 6770, 4380, 121000, 517, 27800, 510, 180000, 4360, 395000, 62…
$ `2010` <dbl> 8460, 4600, 119000, 517, 29100, 524, 188000, 4220, 391000, 67…
$ `2011` <dbl> 12200, 5240, 121000, 491, 30300, 513, 192000, 4920, 392000, 6…
$ `2012` <dbl> 10800, 4910, 130000, 488, 33400, 524, 192000, 5690, 388000, 6…
$ `2013` <dbl> 10000, 5060, 134000, 477, 32600, 524, 190000, 5500, 372000, 6…
$ `2014` <dbl> 9810, 5720, 145000, 462, 34800, 532, 204000, 5530, 361000, 58…
Indicator n
We can see that we have a large tibble. A tibble is the tidyverse version of a data frame. It is essentially a table with variable information arranged as columns, and individual observations arranged as rows. We can see that the tibble gives us information about the class of each variable. For example the country variable is made up of character (abbreviated as chr) values. We see that we have 265 different country variables and CO2 emission values for 192 different years (from 1751 to 2014). Recall that the values are emissions in metric tons also called tonnes. We can see that there are fewer NA values for later years.
Now we will modify this data to make it more usable for making visualizations. One thing we will use is the %<>% opperator which is from the magrittr package. This allows us to use our CO2_emissions data and reassign it to a modified version at the same time.
We will use the pivot_longer() function of the dplyr package to convert our data into what is called long format. This means that we will have more rows and fewer columns than our current format. This is done by collapsing multiple variables into fewer variables.
We want to collapse all of the values for the emission data across the different individual year variables into one new emission variable and we will identify what year they are from using a new Year variable.
CO2_emissions %<>%
pivot_longer(cols = -country, names_to = "Year", values_to = "Emissions")
head(CO2_emissions)# A tibble: 6 x 3
country Year Emissions
<chr> <chr> <dbl>
1 Afghanistan 1751 NA
2 Afghanistan 1752 NA
3 Afghanistan 1753 NA
4 Afghanistan 1754 NA
5 Afghanistan 1755 NA
6 Afghanistan 1756 NA
We also want to rename the country variable to be capitalized. W We can use the rename() function of the dplyr package to rename this variable. When renaming variables the new name is listed first before the =. We will also modify the Emissions data by dividing it by 1000 to make the numbers smaller. To do this we will use the mutate() function, which is also part of the dplyr() package. This function allows us to create and modify variables. You may also note that the Year variable is currently of class type character. We would like to change it to be numeric. This can also be accomplished using the mutate() function.
CO2_emissions %<>%
dplyr::rename(Country=country) %>%
# dplyr::mutate(Emissions = Emissions/1000,
dplyr::mutate(Year = as.numeric(Year),
Label = "CO2 Emissions (Metric Tons)")
#rename(`CO2 Emissions (Mg)`= Emissions)Now let’s take a look to see how our data has changed:
# A tibble: 6 x 4
Country Year Emissions Label
<chr> <dbl> <dbl> <chr>
1 Zimbabwe 2009 5600 CO2 Emissions (Metric Tons)
2 Zimbabwe 2010 7780 CO2 Emissions (Metric Tons)
3 Zimbabwe 2011 9560 CO2 Emissions (Metric Tons)
4 Zimbabwe 2012 7790 CO2 Emissions (Metric Tons)
5 Zimbabwe 2013 11700 CO2 Emissions (Metric Tons)
6 Zimbabwe 2014 12000 CO2 Emissions (Metric Tons)
Great, we can see that now the Year variable is of class double (abbreviated dbl), which is a numeric class.
# A tibble: 6 x 220
country `1801` `1802` `1803` `1804` `1805` `1806` `1807`
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Afghan… NA NA NA NA NA NA NA
2 Albania 0.104 0.104 0.104 0.104 0.104 0.104 0.104
3 Algeria -0.00247 -0.00247 -0.00247 -0.00247 -0.00247 -0.00247 -0.00247
4 Andorra 0.166 0.166 0.166 0.166 0.166 0.166 0.166
5 Angola 0.425 0.425 0.425 0.425 0.425 0.425 0.425
6 Antigu… NA NA NA NA NA NA NA
# … with 212 more variables: `1808` <dbl>, `1809` <dbl>, `1810` <dbl>,
# `1811` <dbl>, `1812` <dbl>, `1813` <dbl>, `1814` <dbl>, `1815` <dbl>,
# `1816` <dbl>, `1817` <dbl>, `1818` <dbl>, `1819` <dbl>, `1820` <dbl>,
# `1821` <dbl>, `1822` <dbl>, `1823` <dbl>, `1824` <dbl>, `1825` <dbl>,
# `1826` <dbl>, `1827` <dbl>, `1828` <dbl>, `1829` <dbl>, `1830` <dbl>,
# `1831` <dbl>, `1832` <dbl>, `1833` <dbl>, `1834` <dbl>, `1835` <dbl>,
# `1836` <dbl>, `1837` <dbl>, `1838` <dbl>, `1839` <dbl>, `1840` <dbl>,
# `1841` <dbl>, `1842` <dbl>, `1843` <dbl>, `1844` <dbl>, `1845` <dbl>,
# `1846` <dbl>, `1847` <dbl>, `1848` <dbl>, `1849` <dbl>, `1850` <dbl>,
# `1851` <dbl>, `1852` <dbl>, `1853` <dbl>, `1854` <dbl>, `1855` <dbl>,
# `1856` <dbl>, `1857` <dbl>, `1858` <dbl>, `1859` <dbl>, `1860` <dbl>,
# `1861` <dbl>, `1862` <dbl>, `1863` <dbl>, `1864` <dbl>, `1865` <dbl>,
# `1866` <dbl>, `1867` <dbl>, `1868` <dbl>, `1869` <dbl>, `1870` <dbl>,
# `1871` <dbl>, `1872` <dbl>, `1873` <dbl>, `1874` <dbl>, `1875` <dbl>,
# `1876` <dbl>, `1877` <dbl>, `1878` <dbl>, `1879` <dbl>, `1880` <dbl>,
# `1881` <dbl>, `1882` <dbl>, `1883` <dbl>, `1884` <dbl>, `1885` <dbl>,
# `1886` <dbl>, `1887` <dbl>, `1888` <dbl>, `1889` <dbl>, `1890` <dbl>,
# `1891` <dbl>, `1892` <dbl>, `1893` <dbl>, `1894` <dbl>, `1895` <dbl>,
# `1896` <dbl>, `1897` <dbl>, `1898` <dbl>, `1899` <dbl>, `1900` <dbl>,
# `1901` <dbl>, `1902` <dbl>, `1903` <dbl>, `1904` <dbl>, `1905` <dbl>,
# `1906` <dbl>, `1907` <dbl>, …
[1] "country" "1801" "1802" "1803" "1804" "1805" "1806"
[8] "1807" "1808" "1809" "1810" "1811" "1812" "1813"
[15] "1814" "1815" "1816" "1817" "1818" "1819" "1820"
[22] "1821" "1822" "1823" "1824" "1825" "1826" "1827"
[29] "1828" "1829" "1830" "1831" "1832" "1833" "1834"
[36] "1835" "1836" "1837" "1838" "1839" "1840" "1841"
[43] "1842" "1843" "1844" "1845" "1846" "1847" "1848"
[50] "1849" "1850" "1851" "1852" "1853" "1854" "1855"
[57] "1856" "1857" "1858" "1859" "1860" "1861" "1862"
[64] "1863" "1864" "1865" "1866" "1867" "1868" "1869"
[71] "1870" "1871" "1872" "1873" "1874" "1875" "1876"
[78] "1877" "1878" "1879" "1880" "1881" "1882" "1883"
[85] "1884" "1885" "1886" "1887" "1888" "1889" "1890"
[92] "1891" "1892" "1893" "1894" "1895" "1896" "1897"
[99] "1898" "1899" "1900" "1901" "1902" "1903" "1904"
[106] "1905" "1906" "1907" "1908" "1909" "1910" "1911"
[113] "1912" "1913" "1914" "1915" "1916" "1917" "1918"
[120] "1919" "1920" "1921" "1922" "1923" "1924" "1925"
[127] "1926" "1927" "1928" "1929" "1930" "1931" "1932"
[134] "1933" "1934" "1935" "1936" "1937" "1938" "1939"
[141] "1940" "1941" "1942" "1943" "1944" "1945" "1946"
[148] "1947" "1948" "1949" "1950" "1951" "1952" "1953"
[155] "1954" "1955" "1956" "1957" "1958" "1959" "1960"
[162] "1961" "1962" "1963" "1964" "1965" "1966" "1967"
[169] "1968" "1969" "1970" "1971" "1972" "1973" "1974"
[176] "1975" "1976" "1977" "1978" "1979" "1980" "1981"
[183] "1982" "1983" "1984" "1985" "1986" "1987" "1988"
[190] "1989" "1990" "1991" "1992" "1993" "1994" "1995"
[197] "1996" "1997" "1998" "1999" "2000" "2001" "2002"
[204] "2003" "2004" "2005" "2006" "2007" "2008" "2009"
[211] "2010" "2011" "2012" "2013" "2014" "2015" "2016"
[218] "2017" "2018" "2019"
Rows: 194
Columns: 220
$ country <chr> "Afghanistan", "Albania", "Algeria", "Andorra", "Angola", "An…
$ `1801` <dbl> NA, 0.10400, -0.00247, 0.16600, 0.42500, NA, NA, NA, 0.21600,…
$ `1802` <dbl> NA, 0.10400, -0.00247, 0.16600, 0.42500, NA, NA, NA, 0.21600,…
$ `1803` <dbl> NA, 0.10400, -0.00247, 0.16600, 0.42500, NA, NA, NA, 0.21600,…
$ `1804` <dbl> NA, 0.10400, -0.00247, 0.16600, 0.42500, NA, NA, NA, 0.21600,…
$ `1805` <dbl> NA, 0.10400, -0.00247, 0.16600, 0.42500, NA, NA, NA, 0.21600,…
$ `1806` <dbl> NA, 0.10400, -0.00247, 0.16600, 0.42500, NA, NA, NA, 0.21600,…
$ `1807` <dbl> NA, 0.10400, -0.00247, 0.16600, 0.42500, NA, NA, NA, 0.21600,…
$ `1808` <dbl> NA, 0.10400, -0.00247, 0.16600, 0.42500, NA, NA, NA, 0.21600,…
$ `1809` <dbl> NA, 0.10400, -0.00247, 0.16600, 0.42500, NA, NA, NA, 0.21600,…
$ `1810` <dbl> NA, 0.10400, -0.00247, 0.16600, 0.42500, NA, NA, NA, 0.21600,…
$ `1811` <dbl> NA, 0.10400, -0.00247, 0.16600, 0.42500, NA, NA, NA, 0.21600,…
$ `1812` <dbl> NA, 0.10400, -0.00247, 0.16600, 0.42500, NA, NA, NA, 0.21600,…
$ `1813` <dbl> NA, 0.10400, -0.00247, 0.16600, 0.42500, NA, NA, NA, 0.21600,…
$ `1814` <dbl> NA, 0.10400, -0.00247, 0.16600, 0.42500, NA, NA, NA, 0.21600,…
$ `1815` <dbl> NA, 0.10400, -0.00247, 0.16600, 0.42500, NA, NA, NA, 0.21600,…
$ `1816` <dbl> NA, 0.10400, -0.00247, 0.16600, 0.42500, NA, NA, NA, 0.21600,…
$ `1817` <dbl> NA, 0.10400, -0.00247, 0.16600, 0.42500, NA, NA, NA, 0.21600,…
$ `1818` <dbl> NA, 0.10400, -0.00247, 0.16600, 0.42500, NA, NA, NA, 0.21600,…
$ `1819` <dbl> NA, 0.10400, -0.00247, 0.16600, 0.42500, NA, NA, NA, 0.21600,…
$ `1820` <dbl> NA, 0.10400, -0.00247, 0.16600, 0.42500, NA, NA, NA, 0.21600,…
$ `1821` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1822` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1823` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1824` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1825` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1826` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1827` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1828` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1829` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1830` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1831` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1832` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1833` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1834` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1835` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1836` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1837` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1838` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1839` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1840` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1841` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1842` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1843` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1844` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1845` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1846` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1847` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1848` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1849` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1850` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1851` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1852` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1853` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1854` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1855` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1856` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1857` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1858` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1859` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1860` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1861` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1862` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1863` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1864` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1865` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1866` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1867` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1868` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1869` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1870` <dbl> 0.32500, 0.21300, 1.02000, 1.17000, 0.42500, 0.66100, 1.41000…
$ `1871` <dbl> 0.325, 1.470, 1.140, 1.170, 0.425, 0.661, 1.410, 0.371, 0.772…
$ `1872` <dbl> 0.3250, 1.4700, 1.1400, 1.1700, 0.4250, 0.6610, 1.4100, 0.371…
$ `1873` <dbl> 0.325, 1.470, 1.140, 1.170, 0.425, 0.661, 1.410, 0.371, 7.600…
$ `1874` <dbl> 0.325, 1.470, 1.140, 1.170, 0.425, 0.661, 1.410, 0.371, 0.292…
$ `1875` <dbl> 0.325, 1.470, 1.140, 1.170, 0.425, 0.661, 1.410, 0.371, 7.910…
$ `1876` <dbl> 0.325, 1.470, 1.140, 1.170, 0.425, 0.661, -0.952, 0.371, -3.1…
$ `1877` <dbl> 0.325, 1.470, 1.140, 1.170, 0.425, 0.661, 7.210, 0.371, 0.720…
$ `1878` <dbl> 0.325, 1.470, 1.140, 1.170, 0.425, 0.661, -8.110, 0.371, 5.98…
$ `1879` <dbl> 0.3250, 1.4700, 1.1400, 1.1700, 0.4250, 0.6610, 1.2700, 0.371…
$ `1880` <dbl> 0.325, 1.470, 1.140, 1.170, 0.425, 0.661, -5.020, 0.371, 1.91…
$ `1881` <dbl> 0.325, 1.470, 1.140, 1.170, 0.425, 0.661, -1.500, 0.371, 3.95…
$ `1882` <dbl> 0.3250, 1.4700, 1.1400, 1.1700, 0.4250, 0.6610, 22.8000, 0.37…
$ `1883` <dbl> 0.325, 1.470, 1.140, 1.170, 0.425, 0.661, 9.000, 0.371, 10.20…
$ `1884` <dbl> 0.325, 1.470, 1.140, 1.170, 0.425, 0.661, 3.980, 0.371, -3.84…
$ `1885` <dbl> 0.3250, 1.4700, 1.1400, 1.1700, 0.4250, 0.6610, 14.2000, 0.37…
$ `1886` <dbl> 0.325, 1.470, 1.140, 1.170, 0.425, 0.661, -2.700, -4.080, -2.…
$ `1887` <dbl> 0.325, 1.470, 1.140, 1.170, 0.425, 0.661, 3.690, 16.700, 6.98…
$ `1888` <dbl> 0.325, 1.470, 1.140, 1.170, 0.425, 0.661, 12.900, -4.020, -2.…
$ `1889` <dbl> 0.325, 1.470, 1.140, 1.170, 0.425, 0.661, 6.590, -7.220, 5.40…
$ `1890` <dbl> 0.325, 1.470, 1.140, 1.170, 0.425, 0.661, -11.300, -0.635, -6…
$ `1891` <dbl> 0.325, 1.370, 1.140, 1.170, 0.425, 0.661, -8.460, -8.610, 4.6…
$ `1892` <dbl> 0.325, 1.370, 1.140, 1.170, 0.425, 0.661, 16.300, 9.230, -14.…
$ `1893` <dbl> 0.325, 1.370, 1.140, 1.170, 0.425, 0.661, 2.840, 13.100, -7.2…
$ `1894` <dbl> 0.325, 1.370, 1.140, 1.170, 0.425, 0.661, 12.200, 13.400, 1.5…
$ `1895` <dbl> 0.325, 1.370, 1.140, 1.170, 0.425, 0.661, 7.760, -7.670, -7.3…
$ `1896` <dbl> 0.325, 1.370, 1.140, 1.170, 0.425, 0.661, 7.520, 9.890, 5.670…
$ `1897` <dbl> 0.325, 1.370, 1.140, 1.170, 0.425, 0.661, -21.900, -1.890, -7…
$ `1898` <dbl> 0.3250, 1.3700, 1.1400, 1.1700, 0.4250, 0.6610, 5.4600, 2.430…
$ `1899` <dbl> 0.325, 1.370, 1.140, 1.170, 0.425, 0.661, 14.700, 5.880, -1.3…
$ `1900` <dbl> 0.3250, 1.3700, 1.1400, 1.1700, 0.4250, 0.6610, -14.8000, -2.…
$ `1901` <dbl> 0.325, 1.310, 1.140, 1.170, 0.425, 0.661, 5.620, 2.270, -4.35…
$ `1902` <dbl> 0.325, 1.310, 1.140, 1.170, 0.425, 0.661, -4.850, 8.420, -0.4…
$ `1903` <dbl> 0.3250, 1.3100, 1.1400, 1.1700, 0.4250, 0.6610, 11.5000, -7.1…
$ `1904` <dbl> 0.325, 1.310, 1.140, 1.170, 0.425, 0.661, 7.830, 10.300, 5.36…
$ `1905` <dbl> 0.325, 1.310, 1.140, 1.170, 0.425, 0.661, 10.400, -11.700, -0…
$ `1906` <dbl> 0.325, 1.310, 1.140, 1.170, 0.425, 0.661, 0.392, -4.640, 5.26…
$ `1907` <dbl> 0.325, 1.310, 1.140, 1.170, 0.425, 0.661, -2.530, -4.130, 2.4…
$ `1908` <dbl> 0.3250, 1.3100, 1.1400, 1.1700, 0.4250, 0.6610, 5.1600, 8.960…
$ `1909` <dbl> 0.325, 1.310, 1.140, 1.170, 0.425, 0.661, 0.294, 3.590, 6.130…
$ `1910` <dbl> 0.325, 1.310, 1.140, 1.170, 0.425, 0.661, 2.640, 6.460, 4.600…
$ `1911` <dbl> 0.32500, 1.28000, 1.14000, 1.17000, 0.42500, 0.66100, -2.2000…
$ `1912` <dbl> 0.32500, 1.28000, 1.14000, 1.17000, 0.42500, 0.66100, 4.17000…
$ `1913` <dbl> 0.32500, 1.28000, 1.14000, 1.17000, 0.42500, 0.66100, -2.9600…
$ `1914` <dbl> 0.463, 0.837, 0.432, 3.800, 2.960, 2.450, -14.300, -4.720, -2…
$ `1915` <dbl> 0.463, 0.837, 0.432, 3.800, 2.960, 2.450, -3.470, 2.690, -2.5…
$ `1916` <dbl> 0.463, 0.837, 0.432, 3.800, 2.960, 2.450, -4.610, -3.150, -0.…
$ `1917` <dbl> 0.463, 0.837, 0.432, 3.800, 2.960, 2.450, -9.830, -16.700, -1…
$ `1918` <dbl> 0.463, 0.837, 0.432, 3.800, 2.960, 2.450, 16.600, -16.700, -3…
$ `1919` <dbl> 0.463, 0.837, 0.432, 3.800, 2.960, 2.450, 1.950, -16.700, 2.4…
$ `1920` <dbl> 0.463, 0.837, 0.432, 3.800, 2.960, 2.450, 5.550, -5.070, 1.06…
$ `1921` <dbl> 0.463, 0.837, 0.432, 3.800, 2.960, 2.450, -0.488, -5.070, 3.0…
$ `1922` <dbl> 0.463, 0.837, 0.432, 3.800, 2.960, 2.450, 4.950, 8.450, 3.110…
$ `1923` <dbl> 0.463, 0.837, 0.432, 3.800, 2.960, 2.450, 7.970, 8.450, 2.540…
$ `1924` <dbl> 0.463, 0.837, 0.432, 3.800, 2.960, 2.450, 4.750, 8.450, 4.340…
$ `1925` <dbl> 0.463, 0.837, 0.432, 3.800, 2.960, 2.450, -3.460, 8.450, 2.50…
$ `1926` <dbl> 0.463, 0.837, 0.432, 3.800, 2.960, 2.450, 2.080, 8.450, 0.351…
$ `1927` <dbl> 0.463, 0.837, 0.432, 3.800, 2.960, 2.450, 4.350, 8.450, -0.51…
$ `1928` <dbl> 0.46300, 0.83700, 0.43200, 3.80000, 2.96000, 2.45000, 3.45000…
$ `1929` <dbl> 0.463, 0.837, 0.432, 3.800, 2.960, 2.450, 1.860, 8.450, -3.46…
$ `1930` <dbl> 0.463, 0.372, 0.432, 3.800, 2.960, 2.450, -6.880, 4.280, -10.…
$ `1931` <dbl> 0.463, 0.372, 0.432, 3.800, 2.960, 2.450, -8.810, 0.680, -7.5…
$ `1932` <dbl> 0.463, 0.372, 0.432, 3.800, 2.960, 2.450, -5.180, -1.760, 4.8…
$ `1933` <dbl> 0.463, 0.372, 0.432, 3.800, 2.960, 2.450, 2.830, 3.530, 6.110…
$ `1934` <dbl> 0.463, 0.372, 0.432, 3.800, 2.960, 2.450, 6.020, 8.930, 4.490…
$ `1935` <dbl> 0.463, 0.372, 0.432, 3.800, 2.960, 2.450, 2.480, 14.100, 5.09…
$ `1936` <dbl> 0.463, 0.372, 0.432, 3.800, 2.960, 2.450, -0.737, 6.560, 3.73…
$ `1937` <dbl> 0.463, 0.372, 0.432, 3.800, 2.960, 2.450, 5.680, 8.050, 4.170…
$ `1938` <dbl> 0.463, 0.372, 0.432, 3.800, 2.960, 2.450, -1.260, -0.505, 2.4…
$ `1939` <dbl> 0.463, 0.372, 0.432, 3.800, 2.960, 2.450, 2.260, 3.820, -0.69…
$ `1940` <dbl> 0.4630, 0.3720, 0.4320, 3.8000, 2.9600, 2.4500, 0.0522, -4.38…
$ `1941` <dbl> 0.463, 0.372, 0.432, 3.800, 2.960, 2.450, 3.670, -2.100, 10.1…
$ `1942` <dbl> 0.463, 0.372, 0.432, 3.800, 2.960, 2.450, -0.457, -2.100, 10.…
$ `1943` <dbl> 0.463, 0.372, 0.432, 3.800, 2.960, 2.450, -2.240, -2.100, 2.6…
$ `1944` <dbl> 0.463, 0.372, 0.432, 3.800, 2.960, 2.450, 9.710, -2.100, -4.4…
$ `1945` <dbl> 0.463, 0.372, 0.432, 3.800, 2.960, 2.450, -4.770, -2.100, -6.…
$ `1946` <dbl> 0.463, 0.372, 0.432, 3.800, 2.960, 2.450, 6.610, -2.100, -4.6…
$ `1947` <dbl> 0.4630, 0.3720, 0.4320, 3.8000, 2.9600, 2.4500, 8.8000, 10.90…
$ `1948` <dbl> 0.463, 0.372, 0.432, 3.800, 2.960, 2.450, 3.160, 12.700, 4.55…
$ `1949` <dbl> 0.463, 0.372, 0.432, 3.800, 2.960, 2.450, -3.620, 8.980, 3.87…
$ `1950` <dbl> 0.463, 0.372, 0.432, 3.800, 2.960, 2.450, -1.110, 8.070, 2.42…
$ `1951` <dbl> 1.250, 4.320, -1.300, 3.800, 2.320, 2.450, 1.750, -1.470, 1.2…
$ `1952` <dbl> 1.660, 0.160, 2.150, 3.800, 2.320, 2.450, -7.090, 4.430, -1.1…
$ `1953` <dbl> 4.290, 4.040, -0.517, 3.800, 2.220, 2.450, 3.320, 2.360, 1.18…
$ `1954` <dbl> 0.3080, 2.9400, 4.9900, 3.8000, -4.1100, 2.4500, 2.2100, 2.87…
$ `1955` <dbl> 0.129, 5.390, 0.573, 3.800, 6.410, 2.450, 5.200, 6.420, 3.040…
$ `1956` <dbl> 2.530, 1.010, 7.460, 3.800, -3.350, 2.450, 0.962, 7.410, 1.01…
$ `1957` <dbl> -1.9400, 6.4100, 9.0300, 3.8000, 7.8600, 2.4500, 3.4200, 0.04…
$ `1958` <dbl> 3.520, 4.500, 1.510, 3.800, 3.620, 2.450, 4.390, 5.390, 2.660…
$ `1959` <dbl> 0.764, 4.120, 16.000, 3.800, -1.230, 2.450, -8.140, -3.080, 3…
$ `1960` <dbl> 1.430, 5.060, 4.750, 3.800, 2.260, 2.450, 6.220, 7.290, 1.880…
$ `1961` <dbl> -1.280, 0.831, -13.800, 3.800, 11.400, 2.450, 5.480, 3.870, -…
$ `1962` <dbl> -0.497, 3.310, -20.400, 3.800, -4.360, 2.450, -3.180, 1.030, …
$ `1963` <dbl> -0.429, 3.410, 23.400, 3.800, 3.380, 2.450, -3.930, -3.740, 4…
$ `1964` <dbl> -0.374, 3.420, 2.160, 3.800, 9.360, 2.450, 8.760, 11.400, 4.7…
$ `1965` <dbl> -0.124, 3.650, 3.530, 3.800, 5.710, 2.450, 7.640, 4.390, 3.08…
$ `1966` <dbl> -1.370, 3.750, -7.730, 3.800, 4.040, 2.450, -0.829, 3.670, 0.…
$ `1967` <dbl> 0.310, 3.760, 5.740, 3.800, 4.040, 2.450, 1.220, 3.310, 4.800…
$ `1968` <dbl> 1.06, 3.61, 8.40, 3.80, -3.19, 2.45, 2.87, 4.81, 3.87, 3.90, …
$ `1969` <dbl> -0.883, 3.400, 6.480, 3.800, 1.150, 2.450, 7.080, 0.450, 3.70…
$ `1970` <dbl> -0.514, 3.690, 6.830, 3.800, 4.510, 2.450, 3.850, 6.690, 4.00…
$ `1971` <dbl> -7.160, 4.000, -11.100, -0.603, 3.880, 4.800, 2.130, 1.650, 2…
$ `1972` <dbl> -4.360, 3.920, 17.500, 2.760, -1.970, 4.720, 0.370, -0.430, 0…
$ `1973` <dbl> 8.580, 4.980, 0.285, 2.630, 5.680, 6.090, 2.020, 7.380, 3.820…
$ `1974` <dbl> 2.750, 0.373, 3.010, 0.870, 0.714, 1.700, 3.700, 1.070, 0.831…
$ `1975` <dbl> 2.5300, 0.3480, 3.8900, -3.5900, -7.3700, -6.2900, -2.2400, -…
$ `1976` <dbl> 2.330, 0.400, 3.420, -0.531, -7.630, -8.910, -1.580, 2.850, 2…
$ `1977` <dbl> -9.2400, 0.4380, 5.7700, -0.6340, -1.8700, 8.2100, 4.8600, 0.…
$ `1978` <dbl> 5.210, 0.460, 9.420, -1.920, -7.870, 4.990, -4.730, 0.760, 1.…
$ `1979` <dbl> -2.180, 0.487, 5.750, -3.670, -2.600, 12.600, 5.440, -2.140, …
$ `1980` <dbl> 0.1680, 0.7060, -1.2600, -2.1000, -0.4710, 8.3500, 0.0163, -1…
$ `1981` <dbl> 10.700, 0.536, -0.656, -4.970, -7.610, 6.330, -6.960, -0.756,…
$ `1982` <dbl> 9.0500, 0.5500, 3.0800, -4.0500, -3.5200, 1.5500, -4.5000, 0.…
$ `1983` <dbl> 3.59000, 0.58400, 1.89000, -3.47000, 0.52000, 8.03000, 2.6700…
$ `1984` <dbl> -1.830, 0.569, 2.270, -2.810, 2.440, 8.850, 0.681, -0.516, 5.…
$ `1985` <dbl> -3.280, 0.523, 2.020, -1.370, 0.316, 9.540, -8.340, -0.847, 3…
$ `1986` <dbl> 7.170, 0.635, -3.790, 0.612, 0.332, 10.700, 5.880, 2.330, 0.6…
$ `1987` <dbl> -17.3000, 0.6290, -3.3100, 3.7300, 4.3100, 11.3000, 1.2500, -…
$ `1988` <dbl> -9.660, 0.633, -4.670, 3.540, 3.020, 10.000, -3.220, 0.453, 2…
$ `1989` <dbl> -2.410, 0.754, 0.771, 2.780, -2.140, 7.540, -8.340, 0.135, 2.…
$ `1990` <dbl> -5.5800, 0.8930, -3.9100, 0.8110, -3.1700, 3.2300, -3.2200, -…
$ `1991` <dbl> -0.572, -28.900, -3.490, -1.470, -2.030, 1.540, 9.290, -13.30…
$ `1992` <dbl> -7.950, -8.100, -0.752, -3.740, -8.830, -0.632, 8.540, -40.80…
$ `1993` <dbl> -13.900, 8.780, -4.440, -5.650, -26.400, 3.080, 4.660, -4.660…
$ `1994` <dbl> -10.400, 7.440, -3.070, -1.650, -1.860, 3.660, 4.770, 8.960, …
$ `1995` <dbl> 20.300, 12.600, 1.710, -0.114, 11.600, -6.630, -3.910, 8.900,…
$ `1996` <dbl> 2.660, 8.650, 1.940, 3.090, 16.600, 3.900, 4.460, 6.080, 2.48…
$ `1997` <dbl> 2.8200, -10.6000, -0.5580, 8.5900, 2.7000, 2.3400, 7.0500, 3.…
$ `1998` <dbl> 2.8300, 12.3000, 3.5000, 3.3000, -2.6300, 1.8200, 2.7900, 7.6…
$ `1999` <dbl> 2.7100, 9.5800, 1.6800, 4.0100, 0.3870, 1.6900, -4.4500, 3.35…
$ `2000` <dbl> -1.0500, 6.7900, 0.9950, 0.4010, -0.0561, -0.6830, -1.8500, 6…
$ `2001` <dbl> -10.400, 6.690, 1.130, 10.000, -0.171, 0.251, -5.470, 10.100,…
$ `2002` <dbl> 22.1000, 2.8600, 2.5500, 3.5800, 10.7000, 0.8110, -12.0000, 1…
$ `2003` <dbl> 8.040, 5.450, 5.460, 4.170, -0.247, 3.670, 7.770, 14.400, 3.0…
$ `2004` <dbl> 2.5000, 5.3600, 3.8400, 4.1800, 7.4500, 5.7500, 7.9200, 10.90…
$ `2005` <dbl> 8.6100, 4.9600, 3.8000, 4.2100, 16.6000, 3.2900, 8.1200, 14.3…
$ `2006` <dbl> 1.590, 5.270, 0.188, 2.370, 15.000, 11.300, 7.250, 13.100, 1.…
$ `2007` <dbl> 10.800, 5.410, 1.850, -1.700, 19.600, 5.780, 7.440, 13.600, 2…
$ `2008` <dbl> 0.117, 6.840, 0.472, -5.600, 10.600, 0.378, 5.570, 6.690, 0.7…
$ `2009` <dbl> 17.300, 2.910, 0.179, -6.310, -0.464, -11.700, -0.276, -15.00…
$ `2010` <dbl> 5.1700, 2.9800, 2.0600, -4.7800, 0.5940, -8.5300, 7.9400, 1.1…
$ `2011` <dbl> 3.8500, 2.4900, 0.8570, -4.3000, 1.0300, -2.9600, 7.6500, 3.6…
$ `2012` <dbl> 11.200, 2.280, 1.160, NA, 2.130, 2.790, 0.761, 6.920, 1.780, …
$ `2013` <dbl> 1.130, 1.720, 1.610, NA, 1.030, 0.468, 3.090, 2.980, 1.170, 0…
$ `2014` <dbl> 0.837, 2.610, 2.180, NA, 2.240, 1.620, -0.622, 4.050, 1.410, …
$ `2015` <dbl> 2.110, 3.820, 2.100, NA, 2.460, 1.900, -0.128, 4.290, 1.480, …
$ `2016` <dbl> 2.680, 4.720, 2.360, NA, 2.770, 2.200, 0.367, 4.490, 1.730, 1…
$ `2017` <dbl> 2.760, 5.030, 2.500, NA, 0.262, 2.200, 0.861, 4.790, 1.700, 1…
$ `2018` <dbl> 3.020, 5.030, 2.630, NA, 3.460, 2.200, 0.861, 4.790, 1.710, 1…
$ `2019` <dbl> 3.380, 5.230, 2.680, NA, 3.550, 2.200, 0.861, 4.790, 1.770, 0…
Again, we will use the pivot_longer() to transform the data to long format. We will also again change the country variable to be Country by using the rename() function , and we will make the Year varaible numeric using the mutate() function.
AVOCADO… this was in Michael’s code… but I don’t know that we need it We will use the drop_na() function of the tidyr package to drop all years with missing data.
gdp_growth %<>%
pivot_longer(cols = -country,
names_to = "Year",
values_to = "gdp_growth") %>%
rename(Country=country) %>%
mutate(Year = as.numeric(Year),
Label = "GDP Growth/Capita (%)") %>%
# tidyr::drop_na() %>% # not sure we need this..
rename(GDP = gdp_growth)Now let’s see how this data has changed:
# A tibble: 6 x 4
Country Year GDP Label
<chr> <dbl> <dbl> <chr>
1 Afghanistan 1801 NA GDP Growth/Capita (%)
2 Afghanistan 1802 NA GDP Growth/Capita (%)
3 Afghanistan 1803 NA GDP Growth/Capita (%)
4 Afghanistan 1804 NA GDP Growth/Capita (%)
5 Afghanistan 1805 NA GDP Growth/Capita (%)
6 Afghanistan 1806 NA GDP Growth/Capita (%)
# A tibble: 219 x 2
Year n
<dbl> <int>
1 1801 194
2 1802 194
3 1803 194
4 1804 194
5 1805 194
6 1806 194
7 1807 194
8 1808 194
9 1809 194
10 1810 194
# … with 209 more rows
Now let’s take a look at the energy use per person data:
# A tibble: 6 x 57
country `1960` `1961` `1962` `1963` `1964` `1965` `1966` `1967` `1968` `1969`
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Albania NA NA NA NA NA NA NA NA NA NA
2 Algeria NA NA NA NA NA NA NA NA NA NA
3 Angola NA NA NA NA NA NA NA NA NA NA
4 Antigu… NA NA NA NA NA NA NA NA NA NA
5 Argent… NA NA NA NA NA NA NA NA NA NA
6 Armenia NA NA NA NA NA NA NA NA NA NA
# … with 46 more variables: `1970` <dbl>, `1971` <dbl>, `1972` <dbl>,
# `1973` <dbl>, `1974` <dbl>, `1975` <dbl>, `1976` <dbl>, `1977` <dbl>,
# `1978` <dbl>, `1979` <dbl>, `1980` <dbl>, `1981` <dbl>, `1982` <dbl>,
# `1983` <dbl>, `1984` <dbl>, `1985` <dbl>, `1986` <dbl>, `1987` <dbl>,
# `1988` <dbl>, `1989` <dbl>, `1990` <dbl>, `1991` <dbl>, `1992` <dbl>,
# `1993` <dbl>, `1994` <dbl>, `1995` <dbl>, `1996` <dbl>, `1997` <dbl>,
# `1998` <dbl>, `1999` <dbl>, `2000` <dbl>, `2001` <dbl>, `2002` <dbl>,
# `2003` <dbl>, `2004` <dbl>, `2005` <dbl>, `2006` <dbl>, `2007` <dbl>,
# `2008` <dbl>, `2009` <dbl>, `2010` <dbl>, `2011` <dbl>, `2012` <dbl>,
# `2013` <dbl>, `2014` <dbl>, `2015` <dbl>
Rows: 169
Columns: 57
$ country <chr> "Albania", "Algeria", "Angola", "Antigua and Barbuda", "Argen…
$ `1960` <dbl> NA, NA, NA, NA, NA, NA, 3060, 1550, NA, NA, NA, NA, NA, NA, 2…
$ `1961` <dbl> NA, NA, NA, NA, NA, NA, 3120, 1550, NA, NA, NA, NA, NA, NA, 2…
$ `1962` <dbl> NA, NA, NA, NA, NA, NA, 3170, 1680, NA, NA, NA, NA, NA, NA, 2…
$ `1963` <dbl> NA, NA, NA, NA, NA, NA, 3280, 1820, NA, NA, NA, NA, NA, NA, 3…
$ `1964` <dbl> NA, NA, NA, NA, NA, NA, 3350, 1860, NA, NA, NA, NA, NA, NA, 3…
$ `1965` <dbl> NA, NA, NA, NA, NA, NA, 3460, 1850, NA, NA, NA, NA, NA, NA, 3…
$ `1966` <dbl> NA, NA, NA, NA, NA, NA, 3550, 1900, NA, NA, NA, NA, NA, NA, 3…
$ `1967` <dbl> NA, NA, NA, NA, NA, NA, 3690, 1920, NA, NA, NA, NA, NA, NA, 3…
$ `1968` <dbl> NA, NA, NA, NA, NA, NA, 3760, 2050, NA, NA, NA, NA, NA, NA, 3…
$ `1969` <dbl> NA, NA, NA, NA, NA, NA, 3790, 2180, NA, NA, NA, NA, NA, NA, 3…
$ `1970` <dbl> NA, NA, NA, NA, NA, NA, 4060, 2420, NA, NA, NA, NA, NA, NA, 4…
$ `1971` <dbl> 785.0, 232.0, 556.0, NA, 1380.0, NA, 3990.0, 2510.0, NA, NA, …
$ `1972` <dbl> 866.0, 261.0, 584.0, NA, 1380.0, NA, 4040.0, 2630.0, NA, NA, …
$ `1973` <dbl> 763.0, 305.0, 568.0, NA, 1410.0, NA, 4260.0, 2830.0, NA, NA, …
$ `1974` <dbl> 777.0, 319.0, 565.0, NA, 1420.0, NA, 4290.0, 2730.0, NA, NA, …
$ `1975` <dbl> 827.0, 330.0, 536.0, NA, 1380.0, NA, 4350.0, 2650.0, NA, NA, …
$ `1976` <dbl> 891, 367, 515, NA, 1400, NA, 4410, 2870, NA, NA, 9580, 98, NA…
$ `1977` <dbl> 924.0, 399.0, 494.0, NA, 1420.0, NA, 4670.0, 2800.0, NA, NA, …
$ `1978` <dbl> 1010.0, 477.0, 527.0, NA, 1430.0, NA, 4630.0, 2890.0, NA, NA,…
$ `1979` <dbl> 864.0, 586.0, 518.0, NA, 1480.0, NA, 4680.0, 3140.0, NA, NA, …
$ `1980` <dbl> 1150, 579, 511, NA, 1490, NA, 4740, 3070, NA, NA, 7790, 103, …
$ `1981` <dbl> 989, 611, 497, NA, 1430, NA, 4690, 2900, NA, NA, 8300, 102, N…
$ `1982` <dbl> 967, 771, 473, NA, 1420, NA, 4820, 2830, NA, NA, 9070, 105, N…
$ `1983` <dbl> 1000, 808, 469, NA, 1420, NA, 4560, 2840, NA, NA, 8500, 105, …
$ `1984` <dbl> 1020, 776, 458, NA, 1450, NA, 4650, 2950, NA, NA, 8830, 104, …
$ `1985` <dbl> 917, 786, 470, NA, 1360, NA, 4600, 3050, NA, NA, 9920, 107, N…
$ `1986` <dbl> 964, 862, 462, NA, 1420, NA, 4620, 3060, NA, NA, 10300, 111, …
$ `1987` <dbl> 922, 828, 461, NA, 1480, NA, 4770, 3170, NA, NA, 9520, 107, N…
$ `1988` <dbl> 928, 850, 467, NA, 1500, NA, 4700, 3200, NA, NA, 10500, 114, …
$ `1989` <dbl> 896, 820, 465, NA, 1440, NA, 5000, 3140, NA, NA, 10200, 117, …
$ `1990` <dbl> 813, 856, 483, 1480, 1410, 2180, 5060, 3240, 3170, 2520, 1060…
$ `1991` <dbl> 573, 884, 480, NA, 1430, 2320, 4930, 3420, 3090, NA, 10100, 1…
$ `1992` <dbl> 418, 884, 467, NA, 1480, 1200, 4960, 3250, 2460, NA, 10800, 1…
$ `1993` <dbl> 412, 868, 468, NA, 1470, 652, 5150, 3260, 2180, NA, 11100, 12…
$ `1994` <dbl> 441, 819, 459, NA, 1540, 420, 5090, 3230, 1950, NA, 11600, 12…
$ `1995` <dbl> 417, 839, 445, NA, 1540, 511, 5130, 3370, 1810, NA, 11400, 13…
$ `1996` <dbl> 448, 798, 445, NA, 1580, 562, 5390, 3580, 1510, NA, 11100, 13…
$ `1997` <dbl> 385, 805, 443, NA, 1610, 594, 5470, 3550, 1440, NA, 12200, 13…
$ `1998` <dbl> 427, 821, 430, NA, 1650, 610, 5550, 3610, 1490, NA, 12400, 13…
$ `1999` <dbl> 576, 864, 439, NA, 1660, 594, 5610, 3590, 1370, NA, 11900, 13…
$ `2000` <dbl> 580, 866, 437, NA, 1660, 656, 5640, 3570, 1400, NA, 12000, 13…
$ `2001` <dbl> 597, 856, 442, NA, 1560, 657, 5450, 3760, 1410, NA, 11700, 14…
$ `2002` <dbl> 660, 904, 447, NA, 1500, 618, 5570, 3770, 1410, NA, 11500, 15…
$ `2003` <dbl> 648, 949, 466, NA, 1590, 657, 5570, 3970, 1480, NA, 11600, 15…
$ `2004` <dbl> 715, 948, 462, 1530, 1720, 698, 5600, 4010, 1540, 2060, 10900…
$ `2005` <dbl> 720, 974, 431, 1530, 1710, 843, 5560, 4090, 1600, 2110, 11700…
$ `2006` <dbl> 707, 1030, 456, 1580, 1840, 865, 5710, 4080, 1560, 2100, 1160…
$ `2007` <dbl> 680, 1070, 470, 1600, 1850, 973, 5870, 4020, 1410, 2070, 1120…
$ `2008` <dbl> 711, 1070, 491, NA, 1920, 1030, 5960, 4030, 1520, NA, 11300, …
$ `2009` <dbl> 732, 1150, 514, NA, 1850, 904, 5860, 3800, 1330, NA, 10300, 1…
$ `2010` <dbl> 729, 1110, 521, NA, 1910, 863, 5790, 4050, 1280, NA, 10200, 2…
$ `2011` <dbl> 765, 1140, 522, NA, 1930, 944, 5750, 3920, 1370, NA, 9910, 20…
$ `2012` <dbl> 688, 1220, 553, NA, 1920, 1030, 5570, 3890, 1470, NA, 9660, 2…
$ `2013` <dbl> 801, 1240, 534, NA, 1950, 1000, 5460, 3920, 1470, NA, 10400, …
$ `2014` <dbl> 808, 1320, 545, NA, 2020, 1020, 5330, 3760, 1500, NA, 10600, …
$ `2015` <dbl> NA, NA, NA, NA, NA, NA, 5480, 3800, NA, NA, NA, NA, NA, NA, 4…
To wrangle the energy_use data, we will again convert the data to long format, rename some variables, and mutate the Year data to be numeric.
energy_use %<>%
pivot_longer(cols = -country,
names_to = "Year",
values_to = "energy_use") %>%
rename(Country = country) %>%
mutate(Year = as.numeric(Year),
Label = "Energy Use (kg, oil-eq./capita)") %>%
rename(Energy = energy_use)# A tibble: 6 x 4
Country Year Energy Label
<chr> <dbl> <dbl> <chr>
1 Zimbabwe 2010 671 Energy Use (kg, oil-eq./capita)
2 Zimbabwe 2011 713 Energy Use (kg, oil-eq./capita)
3 Zimbabwe 2012 735 Energy Use (kg, oil-eq./capita)
4 Zimbabwe 2013 750 Energy Use (kg, oil-eq./capita)
5 Zimbabwe 2014 NA Energy Use (kg, oil-eq./capita)
6 Zimbabwe 2015 NA Energy Use (kg, oil-eq./capita)
# A tibble: 6 x 64
`Data Source` `World Developm… ...3 ...4 ...5 ...6 ...7 ...8 ...9 ...10
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 Last Updated… 43819 <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
2 <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
3 Country Name Country Code Indi… Indi… 1960 1961 1962 1963 1964 1965
4 Aruba ABW Deat… SP.D… 6.38… 6.24… 6.11… 6.01… 5.91… 5.83…
5 Afghanistan AFG Deat… SP.D… 32.2… 31.6… 31.0… 30.5… 30.0… 29.5…
6 Angola AGO Deat… SP.D… 27.0… 26.8… 26.6… 26.4… 26.1… 25.9…
# … with 54 more variables: ...11 <chr>, ...12 <chr>, ...13 <chr>, ...14 <chr>,
# ...15 <chr>, ...16 <chr>, ...17 <chr>, ...18 <chr>, ...19 <chr>,
# ...20 <chr>, ...21 <chr>, ...22 <chr>, ...23 <chr>, ...24 <chr>,
# ...25 <chr>, ...26 <chr>, ...27 <chr>, ...28 <chr>, ...29 <chr>,
# ...30 <chr>, ...31 <chr>, ...32 <chr>, ...33 <chr>, ...34 <chr>,
# ...35 <chr>, ...36 <chr>, ...37 <chr>, ...38 <chr>, ...39 <chr>,
# ...40 <chr>, ...41 <chr>, ...42 <chr>, ...43 <chr>, ...44 <chr>,
# ...45 <chr>, ...46 <chr>, ...47 <chr>, ...48 <chr>, ...49 <chr>,
# ...50 <chr>, ...51 <chr>, ...52 <chr>, ...53 <chr>, ...54 <chr>,
# ...55 <chr>, ...56 <chr>, ...57 <chr>, ...58 <chr>, ...59 <chr>,
# ...60 <chr>, ...61 <chr>, ...62 <chr>, ...63 <chr>, ...64 <chr>
We can see that there are a couple of empty rows which indicate when the data was updated. We can also see that the columns really start at the 3rd row. So first we will repace the column names with the 3rd row. Then we will remove the first 3 rows.
[1] "Data Source" "World Development Indicators"
[3] "...3" "...4"
[5] "...5" "...6"
[7] "...7" "...8"
[9] "...9" "...10"
[11] "...11" "...12"
[13] "...13" "...14"
[15] "...15" "...16"
[17] "...17" "...18"
[19] "...19" "...20"
[21] "...21" "...22"
[23] "...23" "...24"
[25] "...25" "...26"
[27] "...27" "...28"
[29] "...29" "...30"
[31] "...31" "...32"
[33] "...33" "...34"
[35] "...35" "...36"
[37] "...37" "...38"
[39] "...39" "...40"
[41] "...41" "...42"
[43] "...43" "...44"
[45] "...45" "...46"
[47] "...47" "...48"
[49] "...49" "...50"
[51] "...51" "...52"
[53] "...53" "...54"
[55] "...55" "...56"
[57] "...57" "...58"
[59] "...59" "...60"
[61] "...61" "...62"
[63] "...63" "...64"
[1] "Country Name" "Country Code" "Indicator Name" "Indicator Code"
[5] "1960" "1961" "1962" "1963"
[9] "1964" "1965" "1966" "1967"
[13] "1968" "1969" "1970" "1971"
[17] "1972" "1973" "1974" "1975"
[21] "1976" "1977" "1978" "1979"
[25] "1980" "1981" "1982" "1983"
[29] "1984" "1985" "1986" "1987"
[33] "1988" "1989" "1990" "1991"
[37] "1992" "1993" "1994" "1995"
[41] "1996" "1997" "1998" "1999"
[45] "2000" "2001" "2002" "2003"
[49] "2004" "2005" "2006" "2007"
[53] "2008" "2009" "2010" "2011"
[57] "2012" "2013" "2014" "2015"
[61] "2016" "2017" "2018" "2019"
Rows: 264
Columns: 64
$ `Country Name` <chr> "Aruba", "Afghanistan", "Angola", "Albania", "Andorr…
$ `Country Code` <chr> "ABW", "AFG", "AGO", "ALB", "AND", "ARB", "ARE", "AR…
$ `Indicator Name` <chr> "Death rate, crude (per 1,000 people)", "Death rate,…
$ `Indicator Code` <chr> "SP.DYN.CDRT.IN", "SP.DYN.CDRT.IN", "SP.DYN.CDRT.IN"…
$ `1960` <chr> "6.3879999999999999", "32.219000000000001", "27.0970…
$ `1961` <chr> "6.2409999999999997", "31.649000000000001", "26.8590…
$ `1962` <chr> "6.1180000000000003", "31.093", "26.626999999999999"…
$ `1963` <chr> "6.0119999999999996", "30.550999999999998", "26.407"…
$ `1964` <chr> "5.9199999999999999", "30.021999999999998", "26.1939…
$ `1965` <chr> "5.8390000000000004", "29.501000000000001", "25.9660…
$ `1966` <chr> "5.7699999999999996", "28.984999999999999", "25.6900…
$ `1967` <chr> "5.7160000000000002", "28.468", "25.341999999999999"…
$ `1968` <chr> "5.6820000000000004", "27.946000000000002", "24.916"…
$ `1969` <chr> "5.6660000000000004", "27.417999999999999", "24.4179…
$ `1970` <chr> "5.6710000000000003", "26.879999999999999", "23.872"…
$ `1971` <chr> "5.6980000000000004", "26.334", "23.312000000000001"…
$ `1972` <chr> "5.7460000000000004", "25.780999999999999", "22.7770…
$ `1973` <chr> "5.8120000000000003", "25.222000000000001", "22.2959…
$ `1974` <chr> "5.8929999999999998", "24.658000000000001", "21.8850…
$ `1975` <chr> "5.9809999999999999", "24.087", "21.547999999999998"…
$ `1976` <chr> "6.0700000000000003", "23.507999999999999", "21.276"…
$ `1977` <chr> "6.157", "22.920000000000002", "21.047000000000001",…
$ `1978` <chr> "6.2359999999999998", "22.324000000000002", "20.8389…
$ `1979` <chr> "6.3079999999999998", "21.719999999999999", "20.6469…
$ `1980` <chr> "6.3760000000000003", "21.109000000000002", "20.4669…
$ `1981` <chr> "6.444", "20.489999999999998", "20.297999999999998",…
$ `1982` <chr> "6.5190000000000001", "19.864999999999998", "20.145"…
$ `1983` <chr> "6.6020000000000003", "19.239999999999998", "20.009"…
$ `1984` <chr> "6.6929999999999996", "18.617999999999999", "19.8889…
$ `1985` <chr> "6.7850000000000001", "18.004999999999999", "19.7890…
$ `1986` <chr> "6.8730000000000002", "17.405999999999999", "19.7100…
$ `1987` <chr> "6.9480000000000004", "16.826000000000001", "19.651"…
$ `1988` <chr> "7.0049999999999999", "16.268000000000001", "19.6099…
$ `1989` <chr> "7.0430000000000001", "15.738", "19.579000000000001"…
$ `1990` <chr> "7.0590000000000002", "15.241", "19.555", "5.9850000…
$ `1991` <chr> "7.0540000000000003", "14.782999999999999", "19.5330…
$ `1992` <chr> "7.0339999999999998", "14.362", "19.506", "6.1550000…
$ `1993` <chr> "7.0049999999999999", "13.974", "19.463999999999999"…
$ `1994` <chr> "6.9729999999999999", "13.616", "19.396000000000001"…
$ `1995` <chr> "6.9429999999999996", "13.282", "19.292000000000002"…
$ `1996` <chr> "6.9219999999999997", "12.964", "19.146000000000001"…
$ `1997` <chr> "6.9109999999999996", "12.654999999999999", "18.9520…
$ `1998` <chr> "6.915", "12.348000000000001", "18.706", "6.06700000…
$ `1999` <chr> "6.9340000000000002", "12.037000000000001", "18.404"…
$ `2000` <chr> "6.9710000000000001", "11.718", "18.036000000000001"…
$ `2001` <chr> "7.0220000000000002", "11.387", "17.597000000000001"…
$ `2002` <chr> "7.0839999999999996", "11.048", "17.09", "5.891", NA…
$ `2003` <chr> "7.1539999999999999", "10.704000000000001", "16.5219…
$ `2004` <chr> "7.2329999999999997", "10.356", "15.903", "6.0609999…
$ `2005` <chr> "7.3200000000000003", "10.003", "15.24", "6.20600000…
$ `2006` <chr> "7.4180000000000001", "9.6449999999999996", "14.539"…
$ `2007` <chr> "7.5270000000000001", "9.2870000000000008", "13.815"…
$ `2008` <chr> "7.6479999999999997", "8.9320000000000004", "13.0850…
$ `2009` <chr> "7.7800000000000002", "8.5839999999999996", "12.3670…
$ `2010` <chr> "7.9180000000000001", "8.25", "11.68", "6.8410000000…
$ `2011` <chr> "8.0609999999999999", "7.9359999999999999", "11.039"…
$ `2012` <chr> "8.2050000000000001", "7.6449999999999996", "10.4510…
$ `2013` <chr> "8.3469999999999995", "7.3799999999999999", "9.92099…
$ `2014` <chr> "8.4879999999999995", "7.141", "9.4540000000000006",…
$ `2015` <chr> "8.6270000000000007", "6.9290000000000003", "9.05199…
$ `2016` <chr> "8.7650000000000006", "6.742", "8.7159999999999993",…
$ `2017` <chr> "8.907", "6.5750000000000002", "8.4320000000000004",…
$ `2018` <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, …
$ `2019` <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, …
That is looking better! However, we also want to remove some variables like: Country Code, Indicator Name, and Indicator Code. We can do that using the select() functio of the dplyr package. We can use the minus sign - to indicate what variables we dont want to keep. Otherwise, we will perform similar modifications as we performed on the other datasets.
mortality %<>%
select(-`Country Code`,
-`Indicator Name`,
-`Indicator Code`) %>%
rename(Country = "Country Name") %>%
pivot_longer(cols = -Country,
names_to = "Year",
values_to = "Deaths") %>%
mutate(Year = as.numeric(Year),
Deaths = as.numeric(Deaths),
Label = "Deaths/1000 People")# A tibble: 6 x 4
Country Year Deaths Label
<chr> <dbl> <dbl> <chr>
1 Aruba 1960 6.39 Deaths/1000 People
2 Aruba 1961 6.24 Deaths/1000 People
3 Aruba 1962 6.12 Deaths/1000 People
4 Aruba 1963 6.01 Deaths/1000 People
5 Aruba 1964 5.92 Deaths/1000 People
6 Aruba 1965 5.84 Deaths/1000 People
Now we will take a look at the US data about disasters and temperature.
# A tibble: 6 x 57
Year `Drought Count` `Drought Cost` `Drought Lower … `Drought Upper …
<dbl> <dbl> <dbl> <dbl> <dbl>
1 1980 1 33.2 26.4 39.6
2 1981 0 0 0 0
3 1982 0 0 0 0
4 1983 1 7.8 5.5 9
5 1984 0 0 0 0
6 1985 0 0 0 0
# … with 52 more variables: `Drought Lower 90` <dbl>, `Drought Upper 90` <dbl>,
# `Drought Lower 95` <dbl>, `Drought Upper 95` <dbl>, `Flooding Count` <dbl>,
# `Flooding Cost` <dbl>, `Flooding Lower 75` <dbl>, `Flooding Upper
# 75` <dbl>, `Flooding Lower 90` <dbl>, `Flooding Upper 90` <dbl>, `Flooding
# Lower 95` <dbl>, `Flooding Upper 95` <dbl>, `Freeze Count` <dbl>, `Freeze
# Cost` <dbl>, `Freeze Lower 75` <dbl>, `Freeze Upper 75` <dbl>, `Freeze
# Lower 90` <dbl>, `Freeze Upper 90` <dbl>, `Freeze Lower 95` <dbl>, `Freeze
# Upper 95` <dbl>, `Severe Storm Count` <dbl>, `Severe Storm Cost` <dbl>,
# `Severe Storm Lower 75` <dbl>, `Severe Storm Upper 75` <dbl>, `Severe Storm
# Lower 90` <dbl>, `Severe Storm Upper 90` <dbl>, `Severe Storm Lower
# 95` <dbl>, `Severe Storm Upper 95` <dbl>, `Tropical Cyclone Count` <dbl>,
# `Tropical Cyclone Cost` <dbl>, `Tropical Cyclone Lower 75` <dbl>, `Tropical
# Cyclone Upper 75` <dbl>, `Tropical Cyclone Lower 90` <dbl>, `Tropical
# Cyclone Upper 90` <dbl>, `Tropical Cyclone Lower 95` <dbl>, `Tropical
# Cyclone Upper 95` <dbl>, `Wildfire Count` <dbl>, `Wildfire Cost` <dbl>,
# `Wildfire Lower 75` <dbl>, `Wildfire Upper 75` <dbl>, `Wildfire Lower
# 90` <dbl>, `Wildfire Upper 90` <dbl>, `Wildfire Lower 95` <dbl>, `Wildfire
# Upper 95` <dbl>, `Winter Storm Count` <dbl>, `Winter Storm Cost` <dbl>,
# `Winter Storm Lower 75` <dbl>, `Winter Storm Upper 75` <dbl>, `Winter Storm
# Lower 90` <dbl>, `Winter Storm Upper 90` <dbl>, `Winter Storm Lower
# 95` <dbl>, `Winter Storm Upper 95` <dbl>
We are specifically interested in the Year and the variables that contain the word "Count" so we will select them using the select() and contains() functions in the dplyr package. Since we are selecting for variables with the word "Count" we need to use quotation marks around it. Selecting for the variable year does not require this as that is actually the name of one of the existing variables.
# A tibble: 6 x 8
Year `Drought Count` `Flooding Count` `Freeze Count` `Severe Storm C…
<dbl> <dbl> <dbl> <dbl> <dbl>
1 1980 1 1 0 0
2 1981 0 0 1 1
3 1982 0 0 0 2
4 1983 1 2 1 0
5 1984 0 0 0 2
6 1985 0 0 1 0
# … with 3 more variables: `Tropical Cyclone Count` <dbl>, `Wildfire
# Count` <dbl>, `Winter Storm Count` <dbl>
Now we want to create a new variable that will be the sum of all the different types of disasters for each year.
We can create this ne variable using the mutate() function of dplyr and we will use the base rowSums() function to perform the calculation. We dont want to include the Year variable in our sum, so we can exclude it using the selectfunction within the rowSums() function. However, to do so we need to indicate that we are using the data that we already used as input to our mutate() and rowSums() functions. We can do so by using a ..
Rows: 40
Columns: 9
$ Year <dbl> 1980, 1981, 1982, 1983, 1984, 1985, 1986, 19…
$ `Drought Count` <dbl> 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0,…
$ `Flooding Count` <dbl> 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1,…
$ `Freeze Count` <dbl> 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0,…
$ `Severe Storm Count` <dbl> 0, 1, 2, 0, 2, 0, 1, 0, 0, 1, 1, 1, 4, 1, 1,…
$ `Tropical Cyclone Count` <dbl> 1, 0, 0, 1, 0, 3, 0, 0, 0, 1, 0, 1, 2, 0, 1,…
$ `Wildfire Count` <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1,…
$ `Winter Storm Count` <dbl> 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 2,…
$ Disasters <dbl> 3, 2, 3, 5, 2, 5, 2, 0, 1, 5, 3, 4, 7, 5, 6,…
Great, now we are going to remove some of these variables and just keep or select using the select() function the variables we are interested in. We will keep the Flooding Count becuase as you may recall from earlier in this case study, events of extreme perciptation levels appear to be associated with global warming. We will use this as a proxy for that.
AVOCADO why is US listed in 3 variables? We are also going to add a new variable called Country to indicate that this data is from the United States. This will create a new variable where every value is United States. We will also create a new variable called Region where every value is US-specific and a new variable called Type where every value is United States.
us_disaster %<>%
dplyr::select(Year,
Disasters) %>%
mutate(Country = "United States") %>%
pivot_longer(cols = c(-Country, - Year),
names_to = "Indicator",
values_to = "Value") %>%
mutate(Region = "United States",
Type = "US-specific",
Label = "Number of Disasters")
head(us_disaster)# A tibble: 6 x 7
Year Country Indicator Value Region Type Label
<dbl> <chr> <chr> <dbl> <chr> <chr> <chr>
1 1980 United States Disasters 3 United States US-specif… Number of Disast…
2 1981 United States Disasters 2 United States US-specif… Number of Disast…
3 1982 United States Disasters 3 United States US-specif… Number of Disast…
4 1983 United States Disasters 5 United States US-specif… Number of Disast…
5 1984 United States Disasters 2 United States US-specif… Number of Disast…
6 1985 United States Disasters 5 United States US-specif… Number of Disast…
# A tibble: 6 x 3
Date Value Anomaly
<dbl> <dbl> <dbl>
1 189512 50.3 -1.68
2 189612 52.0 -0.03
3 189712 51.6 -0.46
4 189812 51.4 -0.59
5 189912 51.0 -1.01
6 190012 52.8 0.75
OK, so we want to remove the Anomaly variable which is an indicator of how different the national average temperature for that year was from the average temperature from 1901-2000 which was 52.02°F.
We also want to change the date values, which are currently listed as the year followed by the number 12. To do so we want to just keep the first 4 characters in the Date variable string values. We can use the str_sub() function of the stringr package to do this. We just need to indicate the start and stop characters. In this case the start would be 1 and the 4th character would be where we want to stop, so we would use start = 1, stop = 4. Again we will create a Country, Region and Type variable. We will also change the name of the Date variable to Year so that it will be consistent with our other datasets. Furthermore, we also what it to be numeric. We can accomplish both renaming and changing to numeric by using the mutate() function. We canthen remove the Date variable and also order the columns just like the other us data using the select() function.
us_temperature %<>%
dplyr::select(-Anomaly) %>%
mutate(Date = str_sub(Date, start = 1, end = 4))%>%
rename() %>%
mutate(Year = as.numeric(Date),
Country = "United States",
Indicator = "Temperature",
Region = "United States",
Type = "US-specific",
Label = "Temperature (Fahrenheit)") %>%
select(Year, Country, Indicator, Value, Region, Type, Label)
head(us_temperature)# A tibble: 6 x 7
Year Country Indicator Value Region Type Label
<dbl> <chr> <chr> <dbl> <chr> <chr> <chr>
1 1895 United Stat… Temperature 50.3 United Sta… US-speci… Temperature (Fahre…
2 1896 United Stat… Temperature 52.0 United Sta… US-speci… Temperature (Fahre…
3 1897 United Stat… Temperature 51.6 United Sta… US-speci… Temperature (Fahre…
4 1898 United Stat… Temperature 51.4 United Sta… US-speci… Temperature (Fahre…
5 1899 United Stat… Temperature 51.0 United Sta… US-speci… Temperature (Fahre…
6 1900 United Stat… Temperature 52.8 United Sta… US-speci… Temperature (Fahre…
Now we would like to join the different datasets together into one tibble. To do so it is often necessary to have at least one column or variable with the same name to be used as a key for putting your data together. To put all of our data together there are several *_join() functions available in the dplyr package.
We will use the full_join() function as we have different time spans for each dataset and we would like to retain as much data as possible. Thefull_join() function will simply create NA values for any of the years that are not in one of the data sets. We can check by using the base summary() function. This will also allow us to check that there are column names that are consistent in each dataset that we wish to combine.
Country Year Emissions Label
Length:50688 Min. :1751 Min. : 0 Length:50688
Class :character 1st Qu.:1817 1st Qu.: 550 Class :character
Mode :character Median :1882 Median : 4390 Mode :character
Mean :1882 Mean : 83808
3rd Qu.:1948 3rd Qu.: 31925
Max. :2014 Max. :10300000
NA's :33772
Country Year GDP Label
Length:42486 Min. :1801 Min. :-67.500 Length:42486
Class :character 1st Qu.:1855 1st Qu.: 0.133 Class :character
Mode :character Median :1910 Median : 0.633 Mode :character
Mean :1910 Mean : 1.302
3rd Qu.:1965 3rd Qu.: 2.160
Max. :2019 Max. :145.000
NA's :2392
Country Year Energy Label
Length:9464 Min. :1960 Min. : 9.58 Length:9464
Class :character 1st Qu.:1974 1st Qu.: 505.75 Class :character
Mode :character Median :1988 Median : 1185.00 Mode :character
Mean :1988 Mean : 2238.82
3rd Qu.:2001 3rd Qu.: 3030.00
Max. :2015 Max. :22000.00
NA's :3544
Country Year Deaths Label
Length:15840 Min. :1960 Min. : 1.127 Length:15840
Class :character 1st Qu.:1975 1st Qu.: 7.083 Class :character
Mode :character Median :1990 Median : 9.375 Mode :character
Mean :1990 Mean :10.673
3rd Qu.:2004 3rd Qu.:12.722
Max. :2019 Max. :54.444
NA's :1603
Indeed, Country, and Year variables are present in all of the datasets. We can see that the minimum and maximum year is different for nearly all the datasets.
We need to specify what columns/variables we will be joining by using the by = argument in the full_join() function.
df_wide <- CO2_emissions %>%
full_join(gdp_growth, by = c("Country", "Year", "Label")) %>%
full_join(energy_use, by = c("Country", "Year", "Label")) %>%
full_join(mortality, by = c("Country", "Year", "Label"))
df_wide %>%
head()# A tibble: 6 x 7
Country Year Emissions Label GDP Energy Deaths
<chr> <dbl> <dbl> <chr> <dbl> <dbl> <dbl>
1 Afghanistan 1751 NA CO2 Emissions (Metric Tons) NA NA NA
2 Afghanistan 1752 NA CO2 Emissions (Metric Tons) NA NA NA
3 Afghanistan 1753 NA CO2 Emissions (Metric Tons) NA NA NA
4 Afghanistan 1754 NA CO2 Emissions (Metric Tons) NA NA NA
5 Afghanistan 1755 NA CO2 Emissions (Metric Tons) NA NA NA
6 Afghanistan 1756 NA CO2 Emissions (Metric Tons) NA NA NA
We can also do the same thing using by using thereduce() function of the purrr package. This is a good option if you have many dasasets to combine.
df_wide <- list(CO2_emissions,
gdp_growth,
energy_use,
mortality) %>%
reduce(full_join, by = c("Country", "Year", "Label"))
df_wide %>%
head()# A tibble: 6 x 7
Country Year Emissions Label GDP Energy Deaths
<chr> <dbl> <dbl> <chr> <dbl> <dbl> <dbl>
1 Afghanistan 1751 NA CO2 Emissions (Metric Tons) NA NA NA
2 Afghanistan 1752 NA CO2 Emissions (Metric Tons) NA NA NA
3 Afghanistan 1753 NA CO2 Emissions (Metric Tons) NA NA NA
4 Afghanistan 1754 NA CO2 Emissions (Metric Tons) NA NA NA
5 Afghanistan 1755 NA CO2 Emissions (Metric Tons) NA NA NA
6 Afghanistan 1756 NA CO2 Emissions (Metric Tons) NA NA NA
Rows: 118,478
Columns: 7
$ Country <chr> "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",…
$ Year <dbl> 1751, 1752, 1753, 1754, 1755, 1756, 1757, 1758, 1759, 1760,…
$ Emissions <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
$ Label <chr> "CO2 Emissions (Metric Tons)", "CO2 Emissions (Metric Tons)…
$ GDP <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
$ Energy <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
$ Deaths <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
Nice, looks good!
We will also make a long version of this data, where we will create an new variable called Indicator that will indicate what dataset the data came from and we will collapse the values from the columns called Emissions (CO2 Emissions (Mg)), GDP(GDP Growth/Capita (%)), Energy(Energy Use (kg, oil-eq./capita)), and Deaths (Deaths/1000 People).
df_long <- df_wide %>%
pivot_longer(cols = c(-Country, -Year, - Label),
names_to = "Indicator",
values_to = "Value")
head(df_long)# A tibble: 6 x 5
Country Year Label Indicator Value
<chr> <dbl> <chr> <chr> <dbl>
1 Afghanistan 1751 CO2 Emissions (Metric Tons) Emissions NA
2 Afghanistan 1751 CO2 Emissions (Metric Tons) GDP NA
3 Afghanistan 1751 CO2 Emissions (Metric Tons) Energy NA
4 Afghanistan 1751 CO2 Emissions (Metric Tons) Deaths NA
5 Afghanistan 1752 CO2 Emissions (Metric Tons) Emissions NA
6 Afghanistan 1752 CO2 Emissions (Metric Tons) GDP NA
We will also create a new variable called Region that will indicate if the data is about the United States or a different country based on the values in the Country variable. We will use the case_when() function of the dplyr package to do this. If the Country variable is equal to "United States" the value for the new variable will also be “United States”, where as if the Country variable is not equal to "United States" but is some other character string value, such as "Afghanistan", then the value for the new variable will be "Rest of the World". The new values for the new variable Region are indicated after the specific conditional statements by using the ~ symbol. We will also create a new variable called Type, where all the values are "Global" to indicate that this data is not specific to just the United States.
df_long %<>%
mutate(Region = case_when(Country == "United States" ~ "United States",
Country != "United States" ~ "Rest of the World"),
Type = "Global")
head(df_long)# A tibble: 6 x 7
Country Year Label Indicator Value Region Type
<chr> <dbl> <chr> <chr> <dbl> <chr> <chr>
1 Afghanistan 1751 CO2 Emissions (Metric … Emissions NA Rest of the W… Glob…
2 Afghanistan 1751 CO2 Emissions (Metric … GDP NA Rest of the W… Glob…
3 Afghanistan 1751 CO2 Emissions (Metric … Energy NA Rest of the W… Glob…
4 Afghanistan 1751 CO2 Emissions (Metric … Deaths NA Rest of the W… Glob…
5 Afghanistan 1752 CO2 Emissions (Metric … Emissions NA Rest of the W… Glob…
6 Afghanistan 1752 CO2 Emissions (Metric … GDP NA Rest of the W… Glob…
We will now combine this data with the US data about disasters and temperatures.
We will now use the bind_rows() function which will just append the us_temperature data and the us_disaster data after the df_long data.
# A tibble: 6 x 7
Year Country Indicator Value Region Type Label
<dbl> <chr> <chr> <dbl> <chr> <chr> <chr>
1 1980 United States Disasters 3 United States US-specif… Number of Disast…
2 1981 United States Disasters 2 United States US-specif… Number of Disast…
3 1982 United States Disasters 3 United States US-specif… Number of Disast…
4 1983 United States Disasters 5 United States US-specif… Number of Disast…
5 1984 United States Disasters 2 United States US-specif… Number of Disast…
6 1985 United States Disasters 5 United States US-specif… Number of Disast…
# A tibble: 6 x 7
Year Country Indicator Value Region Type Label
<dbl> <chr> <chr> <dbl> <chr> <chr> <chr>
1 1895 United Stat… Temperature 50.3 United Sta… US-speci… Temperature (Fahre…
2 1896 United Stat… Temperature 52.0 United Sta… US-speci… Temperature (Fahre…
3 1897 United Stat… Temperature 51.6 United Sta… US-speci… Temperature (Fahre…
4 1898 United Stat… Temperature 51.4 United Sta… US-speci… Temperature (Fahre…
5 1899 United Stat… Temperature 51.0 United Sta… US-speci… Temperature (Fahre…
6 1900 United Stat… Temperature 52.8 United Sta… US-speci… Temperature (Fahre…
df_long <-list(df_long,
us_disaster,
us_temperature) %>%
bind_rows()
df_long$Country <- as.factor(df_long$Country)We can check the top and bottom of the new df_long tibble to see that our us_temperature data is at the bottom.
# A tibble: 6 x 7
Country Year Label Indicator Value Region Type
<fct> <dbl> <chr> <chr> <dbl> <chr> <chr>
1 Afghanistan 1751 CO2 Emissions (Metric … Emissions NA Rest of the W… Glob…
2 Afghanistan 1751 CO2 Emissions (Metric … GDP NA Rest of the W… Glob…
3 Afghanistan 1751 CO2 Emissions (Metric … Energy NA Rest of the W… Glob…
4 Afghanistan 1751 CO2 Emissions (Metric … Deaths NA Rest of the W… Glob…
5 Afghanistan 1752 CO2 Emissions (Metric … Emissions NA Rest of the W… Glob…
6 Afghanistan 1752 CO2 Emissions (Metric … GDP NA Rest of the W… Glob…
# A tibble: 6 x 7
Country Year Label Indicator Value Region Type
<fct> <dbl> <chr> <chr> <dbl> <chr> <chr>
1 United Stat… 2014 Temperature (Fahren… Temperatu… 52.5 United Sta… US-speci…
2 United Stat… 2015 Temperature (Fahren… Temperatu… 54.4 United Sta… US-speci…
3 United Stat… 2016 Temperature (Fahren… Temperatu… 54.9 United Sta… US-speci…
4 United Stat… 2017 Temperature (Fahren… Temperatu… 54.6 United Sta… US-speci…
5 United Stat… 2018 Temperature (Fahren… Temperatu… 53.5 United Sta… US-speci…
6 United Stat… 2019 Temperature (Fahren… Temperatu… 52.7 United Sta… US-speci…
Click here for details about the difference between
full_join() and bind_rows()
The difference between this function and the full_join() function is that the bind_rows() function will assentially just append each dataset to each other, whereas the full_join() function collapses data that is comparable. Here you will see an example of what the data would have been like for df_wide if we had made it using bind_rows(). We will specifically look at the values for China in the year of 1960.
[1] 118478 7
[1] 118478 7
# A tibble: 4 x 7
Country Year Emissions Label GDP Energy Deaths
<chr> <dbl> <dbl> <chr> <dbl> <dbl> <dbl>
1 China 1960 781000 CO2 Emissions (Metric Tons) NA NA NA
2 China 1960 NA GDP Growth/Capita (%) -6.45 NA NA
3 China 1960 NA Energy Use (kg, oil-eq./capita) NA NA NA
4 China 1960 NA Deaths/1000 People NA NA 25.4
# A tibble: 4 x 7
Country Year Emissions Label GDP Energy Deaths
<chr> <dbl> <dbl> <chr> <dbl> <dbl> <dbl>
1 China 1960 781000 CO2 Emissions (Metric Tons) NA NA NA
2 China 1960 NA GDP Growth/Capita (%) -6.45 NA NA
3 China 1960 NA Energy Use (kg, oil-eq./capita) NA NA NA
4 China 1960 NA Deaths/1000 People NA NA 25.4
To remove entries for countries with NA values we can use filter(complete.cases(.)).
df_long_with_miss <- df_long %>%
arrange(Country)
df_long<- df_long %>%
filter(complete.cases(.)) %>%
arrange(Country)
head(df_long_with_miss)# A tibble: 6 x 7
Country Year Label Indicator Value Region Type
<fct> <dbl> <chr> <chr> <dbl> <chr> <chr>
1 Afghanistan 1751 CO2 Emissions (Metric … Emissions NA Rest of the W… Glob…
2 Afghanistan 1751 CO2 Emissions (Metric … GDP NA Rest of the W… Glob…
3 Afghanistan 1751 CO2 Emissions (Metric … Energy NA Rest of the W… Glob…
4 Afghanistan 1751 CO2 Emissions (Metric … Deaths NA Rest of the W… Glob…
5 Afghanistan 1752 CO2 Emissions (Metric … Emissions NA Rest of the W… Glob…
6 Afghanistan 1752 CO2 Emissions (Metric … GDP NA Rest of the W… Glob…
# A tibble: 6 x 7
Country Year Label Indicator Value Region Type
<fct> <dbl> <chr> <chr> <dbl> <chr> <chr>
1 Afghanistan 1949 CO2 Emissions (Metric … Emissions 14.7 Rest of the W… Glob…
2 Afghanistan 1950 CO2 Emissions (Metric … Emissions 84.3 Rest of the W… Glob…
3 Afghanistan 1951 CO2 Emissions (Metric … Emissions 91.7 Rest of the W… Glob…
4 Afghanistan 1952 CO2 Emissions (Metric … Emissions 91.7 Rest of the W… Glob…
5 Afghanistan 1953 CO2 Emissions (Metric … Emissions 106 Rest of the W… Glob…
6 Afghanistan 1954 CO2 Emissions (Metric … Emissions 106 Rest of the W… Glob…
Now we will create some simple plots to examine the data.
We can check the time span of this data by refering back to the What are the data? section. To make these plots we will use the ggplot2 package. The first step in creating a plot is to define what data we intend to use and what data will be ploted on the x-axis, the y-axis, and if any data will be used to determine the color or the fill (also color of plots that have something to fill like a bar plot) or group. All of these are defined using the aes() argument, which is short for aesthetic mappings.
First we will take a look at the CO2 emission data.
We first need to give the correct data input. We will filter our data to only include the CO2 emissions data by using the filter() function of the dplyr package. To use this function we need to specify what value we want for a given variable. In this case we want all rows where the Indicator variable is equal to the word Emissions. Notice that this needs to be in quotes, while the variable name does not.
Then we use the aes() argument of the ggplot() function to define that our x-axis will be the Year variable, the y-axis will be the emission Value variable, and that our data should be grouped or separted by the Country variable. If we were to stop there we would get a blank plot, as you can see below. We need to add another layer to define how we want the plot to look. We do so by using the + sign in between each command.
We will use the
geom_line() function becuase we would like to create a line plot. There are many geom_* functions to choose from that create many different types of plots.
Type geom into the RStudio console and you will see many options to scroll through.
Since we have many overlapping lines, we will make our lines slightly transparent by using the
alpha argument. This takes values from 0 to 1, where 0 is completely transparent and 1 is completely opague. We will also add labels using the labs() function. Again, notice that a plus sign is used between each layer that we add to the plot. To make CO2 appear with a subscript we can use ~CO[2]~. We will also use the function theme_linedraw() of ggplot2 to change the general apearance of the plot.
Type theme_ in the RStudio console to see the varios plot theme options available.
We will also use the theme() function to change the font size of the x-axis, y-axis, axis titles, and the caption as shown below. To know what to call each element of the plot in this function to change the size type ?theme() in the console. You will see a very large list that includes other plot aspects like the background and the legend. This function can be used to modify your plot to your specifications.
df_long %>%
filter(Indicator == "Emissions") %>%
ggplot(aes(x = Year, y = Value, group = Country))+
geom_line(alpha = 0.2) +
labs(title = "Country" ~CO[2]~ "Emissions per Year, 1751-2014",
caption = "Limited to reporting countries",
y = "Emissions (1M Metric Tons)") +
geom_line(data = df_long %>%
filter(Indicator == "Emissions",
Country == "United States"), aes(x=Year, y=Value, color = Country)) +
scale_colour_manual(values=c("red")) +
theme_linedraw() +
theme(axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 12),
axis.title.x = element_text(size = 12),
axis.title.y = element_text(size = 12),
plot.caption = element_text(size = 12),
plot.title = element_text(size = 16),
legend.title = element_blank())Great! We’ve created our first plot. We can see that many countries show a dramatic increase in emissions over time with a handful of countries with particularly high levels.
Now let’s take a look at the data in slightly different way. Let’s look at overall global emissions by calculating a sum each year of all the emission values for the different countries. Note that this is limited to only the countries included in the dataset.
To calculate this value we will first use the group_by() function of the dplyr package. This will allow our calcluation to be performed on aggregated data by the different values for the Year variable. Otherwise, we would simply get a sum of overall emissions across all of the years in the data set.
Then we will use the summarize() function (also of the dplyr package) and the base sum() function to calculate a sum of the emission values each year.
Since we will be ploting only one value each year, we no longer need to assign a group in the aes() argument. this time we will make the size of the line that will be plotted a bit larger using the size() argument in the geom_line() function.
CO2_world<-df_long %>%
filter(Indicator == "Emissions") %>%
group_by(Year) %>%
summarize(Value = sum(Value)) %>%
ggplot(aes(x = Year, y = Value)) +
geom_line(size = 1.5) +
labs(title = "World "~CO[2]~" Emissions per Year , 1751-2014",
caption = "Limited to reporting countries",
y = "Emissions (Metric Tonnes)")+
theme_linedraw() +
theme(axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 12),
axis.title.x = element_text(size = 12),
axis.title.y = element_text(size = 12),
plot.caption = element_text(size = 12),
plot.title = element_text(size = 16))
CO2_worldOk, we can now clearly see that global CO2 emissions have dramatically risen since 1900.
Now we will take a look a GDP growth of various countries
df_long %>%
filter(Indicator == "GDP") %>%
ggplot(aes(x = Year, y = Value, group = Country)) +
geom_line(alpha = 0.2) +
labs(title = "Country GDP Growth per Capita per Year (Annual %), 1801-2019",
caption = "Limited to reporting countries",
y = "GDP Growth per Capita (Annual %)") +
geom_line(data = df_long %>%
filter(Indicator == "GDP",
Country == "United States"), aes(x=Year, y=Value, color = Country)) +
scale_colour_manual(values=c("red")) +
theme_linedraw() +
theme(axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 12),
axis.title.x = element_text(size = 12),
axis.title.y = element_text(size = 12),
plot.caption = element_text(size = 12),
plot.title = element_text(size = 16),
legend.title = element_blank()) We can see that the variation in GDP has become greater over time.
df_long %>%
filter(Indicator == "GDP",
Year >= 1801) %>%
group_by(Year) %>%
summarise(Value = mean(Value, na.rm = TRUE)) %>%
ggplot(aes(x=Year, y=Value)) +
geom_line() +
labs(title = "Mean Country GDP Growth per Capita per Year (Annual %), 1801-2019",
caption = "Limited to reporting countries",
y = "GDP Growth per Capita (Annual %)") +
theme_linedraw() +
theme(axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 12),
axis.title.x = element_text(size = 12),
axis.title.y = element_text(size = 12),
plot.caption = element_text(size = 12),
plot.title = element_text(size = 16))df_long %>%
filter(Indicator == "Energy",
Year >= 1960,
Year <= 2015) %>%
ggplot(aes(x=Year, y= Value, group=Country)) +
geom_line(alpha=0.2) +
geom_line(data = df_long %>%
filter(Indicator == "Energy",
Country == "United States"), aes(x=Year, y=Value, color = Country)) +
scale_colour_manual(values=c("red")) +
labs(title = "Country Energy Use (kg of Oil Equivalent per Capita), 1960 to 2015",
caption = "Limited to reporting countries",
y = "Energy Use (kg of Oil Equivalent per Capita)")+
theme_linedraw() +
theme(axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 12),
axis.title.x = element_text(size = 12),
axis.title.y = element_text(size = 12),
plot.caption = element_text(size = 12),
plot.title = element_text(size = 15),
legend.title = element_blank())df_long %>%
filter(Indicator == "Energy",
Year >= 1960,
Year <= 2015) %>%
group_by(Year) %>%
summarise(Value = sum(Value, na.rm = TRUE)) %>%
ggplot(aes(x=Year, y=Value)) +
geom_line() +
labs(title = "Worldwide Energy Use (kg of Oil Equivalent per Capita), 1960 to 2015",
caption = "Limited to reporting countries",
y = "Energy Use (kg of Oil Equivalent per Capita)")+
theme_linedraw() +
theme(axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 12),
axis.title.x = element_text(size = 12),
axis.title.y = element_text(size = 12),
plot.caption = element_text(size = 12),
plot.title = element_text(size = 15))Mortality <-df_long %>%
filter(Indicator == "Deaths",
Year >= 1960,
Year <= 2019) %>%
ggplot(aes(x=Year, y=Value, group=Country)) +
geom_line(alpha=0.2) +
geom_line(data =df_long %>%
filter(Indicator == "Deaths",
Country == "United States",
Year >= 1960,
Year <= 2019), aes(x=Year, y=Value, color = Country)) +
scale_colour_manual(values=c("red")) +
labs(title = "Country Crude Mortality Rate (per 1000 Persons), 1960 to 2019",
caption = "Limited to reporting countries",
y = "Crude Mortality Rate (per 1000 Persons)")+
theme_linedraw() +
theme(axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 12),
axis.title.x = element_text(size = 12),
axis.title.y = element_text(size = 12),
plot.caption = element_text(size = 12),
plot.title = element_text(size = 16),
legend.title = element_blank())
Mortality # A tibble: 244 x 7
Country Year Label Indicator Value Region Type
<fct> <dbl> <chr> <chr> <dbl> <chr> <chr>
1 Cambodia 1980 Deaths/1000 People Deaths 43.9 Rest of the Wor… Glob…
2 Timor-Leste 1980 Deaths/1000 People Deaths 26.7 Rest of the Wor… Glob…
3 Niger 1980 Deaths/1000 People Deaths 26.0 Rest of the Wor… Glob…
4 Mali 1980 Deaths/1000 People Deaths 25.2 Rest of the Wor… Glob…
5 Sierra Leone 1980 Deaths/1000 People Deaths 24.8 Rest of the Wor… Glob…
6 South Sudan 1980 Deaths/1000 People Deaths 24.6 Rest of the Wor… Glob…
7 Guinea 1980 Deaths/1000 People Deaths 23.4 Rest of the Wor… Glob…
8 Guinea-Bissau 1980 Deaths/1000 People Deaths 21.5 Rest of the Wor… Glob…
9 Malawi 1980 Deaths/1000 People Deaths 21.5 Rest of the Wor… Glob…
10 Mozambique 1980 Deaths/1000 People Deaths 21.5 Rest of the Wor… Glob…
# … with 234 more rows
# A tibble: 248 x 7
Country Year Label Indicator Value Region Type
<fct> <dbl> <chr> <chr> <dbl> <chr> <chr>
1 Rwanda 1995 Deaths/1000 People Deaths 34.4 Rest of the World Glob…
2 Sierra Leone 1995 Deaths/1000 People Deaths 26.1 Rest of the World Glob…
3 Niger 1995 Deaths/1000 People Deaths 20.2 Rest of the World Glob…
4 Mali 1995 Deaths/1000 People Deaths 19.7 Rest of the World Glob…
5 Angola 1995 Deaths/1000 People Deaths 19.3 Rest of the World Glob…
6 Uganda 1995 Deaths/1000 People Deaths 18.8 Rest of the World Glob…
7 Malawi 1995 Deaths/1000 People Deaths 18.5 Rest of the World Glob…
8 Somalia 1995 Deaths/1000 People Deaths 18.4 Rest of the World Glob…
9 Nigeria 1995 Deaths/1000 People Deaths 18.4 Rest of the World Glob…
10 Chad 1995 Deaths/1000 People Deaths 18.4 Rest of the World Glob…
# … with 238 more rows
# A tibble: 248 x 7
Country Year Label Indicator Value Region Type
<fct> <dbl> <chr> <chr> <dbl> <chr> <chr>
1 Bulgaria 2017 Deaths/1000 People Deaths 15.5 Rest of the World Global
2 Latvia 2017 Deaths/1000 People Deaths 14.8 Rest of the World Global
3 Serbia 2017 Deaths/1000 People Deaths 14.8 Rest of the World Global
4 Lesotho 2017 Deaths/1000 People Deaths 14.7 Rest of the World Global
5 Ukraine 2017 Deaths/1000 People Deaths 14.5 Rest of the World Global
6 Lithuania 2017 Deaths/1000 People Deaths 14.2 Rest of the World Global
7 Hungary 2017 Deaths/1000 People Deaths 13.5 Rest of the World Global
8 Romania 2017 Deaths/1000 People Deaths 13.3 Rest of the World Global
9 Croatia 2017 Deaths/1000 People Deaths 13 Rest of the World Global
10 Georgia 2017 Deaths/1000 People Deaths 12.9 Rest of the World Global
# … with 238 more rows
df_long %>%
filter(Indicator == "Deaths",
Country == "Cambodia") %>%
ggplot(aes(x=Year, y=Value)) +
geom_line() +
labs(title = "Cambodia Crude Mortality Rate (per 1000 Persons), 1960 to 2019",
caption = "Limited to reporting countries",
y = "Crude Mortality Rate (per 1000 Persons)")+
theme_linedraw() +
theme(axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 12),
axis.title.x = element_text(size = 12),
axis.title.y = element_text(size = 12),
plot.caption = element_text(size = 12),
plot.title = element_text(size = 16))df_long %>%
filter(Indicator == "Deaths",
Country == "Rwanda") %>%
ggplot(aes(x=Year, y=Value)) +
geom_line() +
labs(title = "Rwanda Crude Mortality Rate (per 1000 Persons), 1960 to 2019",
caption = "Limited to reporting countries",
y = "Crude Mortality Rate (per 1000 Persons)")+
theme_linedraw() +
theme(axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 12),
axis.title.x = element_text(size = 12),
axis.title.y = element_text(size = 12),
plot.caption = element_text(size = 12),
plot.title = element_text(size = 16))df_long %>%
filter(Indicator == "Deaths",
Country %in% c("United States", "Rwanda", "Cambodia", "Qatar", "Niger", "Israel", "Mexico", "China"))%>%
ggplot(aes(x=Year, y=Value)) +
geom_line() +
facet_grid(~ Country) labs(title = " Crude Mortality Rate (per 1000 Persons), 1960 to 2019",
caption = "Limited to reporting countries",
y = "Crude Mortality Rate (per 1000 Persons)")+
theme_linedraw() +
theme(axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 12),
axis.title.x = element_text(size = 12),
axis.title.y = element_text(size = 12),
plot.caption = element_text(size = 12),
plot.title = element_text(size = 16))NULL
df_long %>%
filter(Indicator == "Deaths",
Country %in% c("United States", "Rwanda", "Cambodia", "Qatar", "Niger", "Israel", "Mexico", "China"))%>%
ggplot(aes(x=Year, y=Value, color = Country)) +
geom_line() +
labs(title = " Crude Mortality Rate (per 1000 Persons), 1960 to 2019",
caption = "Limited to reporting countries",
y = "Crude Mortality Rate (per 1000 Persons)")+
theme_linedraw() +
theme(axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 12),
axis.title.x = element_text(size = 12),
axis.title.y = element_text(size = 12),
plot.caption = element_text(size = 12),
plot.title = element_text(size = 16))df_long %>%
filter(Indicator == "Deaths",
Country %in% c("United States", "Rwanda", "Cambodia", "Qatar", "Niger", "Israel", "Mexico", "China"))%>%
filter(Year > 2015) %>%
ggplot(aes(x=Year, y=Value, color = Country)) +
geom_line() +
labs(title = " Crude Mortality Rate (per 1000 Persons), 1960 to 2019",
caption = "Limited to reporting countries",
y = "Crude Mortality Rate (per 1000 Persons)")+
theme_linedraw() +
theme(axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 12),
axis.title.x = element_text(size = 12),
axis.title.y = element_text(size = 12),
plot.caption = element_text(size = 12),
plot.title = element_text(size = 16))df_long %>%
filter(Indicator == "Deaths",
Year >= 1960,
Year <= 2019) %>%
group_by(Year) %>%
summarise(Value = mean(Value, na.rm = TRUE)) %>%
ggplot(aes(x=Year, y=Value)) +
geom_line(size = 1.4) +
labs(title = "Mean Country Crude Mortality Rate (per 1000 Persons), 1960 to 2018",
caption = "Limited to reporting countries",
y = "Crude Mortality Rate (per 1000 Persons)")+
theme_linedraw() +
theme(axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 12),
axis.title.x = element_text(size = 12),
axis.title.y = element_text(size = 12),
plot.caption = element_text(size = 12),
plot.title = element_text(size = 16))df_long %>%
filter(Indicator == "Disasters",
Year >= 1980,
Year <= 2019) %>%
ggplot(aes(x=Year, y=Value, group=Country)) +
geom_line() +
geom_smooth(method = "loess") +
labs(title = "US Disasters, 1980 to 2019",
subtitle = "Drougths, Floods, Freezes, Severe Storms. Tropical Cyclones, Wildfires, and Winter Storms",
y = "Disaster Count")+
theme_linedraw() +
theme(axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 12),
axis.title.x = element_text(size = 12),
axis.title.y = element_text(size = 12),
plot.title = element_text(size = 16))df_long %>%
filter(Indicator == "Temperature",
Year >= 1895,
Year <= 2019) %>%
ggplot(aes(x=Year, y=Value, group=Country)) +
geom_line() +
geom_smooth(method = "loess") +
labs(title = "US Average Annual Temperature, 1895 to 2019",
y = "Temperature (Fahrenheit)")+
theme_linedraw() +
theme(axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 12),
axis.title.x = element_text(size = 12),
axis.title.y = element_text(size = 12),
plot.title = element_text(size = 16))Now Let’s try putting the data together.
ggplot(df_long, aes(x=Year, y=Value, group=Country)) +
geom_line(alpha=0.2) +
geom_line(data = df_long %>%
filter(Country == "United States"), aes(x=Year, y=Value, color = Country)) +
scale_colour_manual(values=c("red")) +
facet_wrap(Indicator~., scales = "free_y",strip.position="right", ncol = 1) +
labs(title="Distribution of Indicators by Year and Value",
y = "Indicator Value")+
theme_linedraw() +
theme(axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 12),
axis.title.x = element_text(size = 12),
axis.title.y = element_text(size = 12),
plot.caption = element_text(size = 12),
plot.title = element_text(size = 16))This looks a bit awkward, because the eacy type of data spans a different time spans.
Let’s take a look at the reporting countries for each year for each type of data.
df_long %>%
filter(Type == "Global") %>%
group_by(Year, Label, .drop=FALSE) %>%
tally() %>%
ggplot(aes(x= Year, y = n, color = Label)) +
geom_line() +
geom_vline(xintercept = 1980, linetype=2, color="black") +
geom_vline(xintercept = 2014, linetype=2, color="black") +
labs(title = "Countries with Complete Data per Year",
subtitle = "Global Data",
y = "Countries") +
scale_x_continuous(breaks = seq(1750,2020,by=10),
labels = seq(1750,2020,by=10)) +
theme(axis.text.x = element_text(angle = 90),
axis.title.x = element_blank(),
legend.position = "bottom")+
theme_linedraw() +
theme(axis.text.x = element_text(size = 12, angle = 90),
axis.text.y = element_text(size = 12),
axis.title.x = element_text(size = 12),
axis.title.y = element_text(size = 12),
plot.caption = element_text(size = 12),
plot.title = element_text(size = 16),
legend.title = element_blank())We can see that all of our data spans from 1980 to 2014.
df_long %>%
filter(Region=="United States") %>%
group_by(Label) %>%
summarise(Start=min(Year), End=max(Year)) %>%
ggplot(aes(x=Label, y=End)) +
geom_hline(yintercept = 1980, linetype=2, color="black") +
geom_hline(yintercept = 2014, linetype=2, color="black") +
geom_segment(aes(x=Label,
xend=Label,
yend=End,
y=Start)) +
geom_point(aes(x=Label, y=Start), shape=16, color="black") +
geom_point(aes(x=Label, y=End), shape=21, fill="white", color="black") +
coord_flip() +
labs(title = "Complete Data per Year",
subtitle = "US-specific Data",
y = "Countries") +
scale_y_continuous(breaks = seq(1750,2020,by=10),
labels = seq(1750,2020,by=10)) +
theme_linedraw() +
theme(axis.text.x = element_text(size = 12,angle = 90),
axis.text.y = element_text(size = 12),
axis.title = element_blank(),
plot.caption = element_text(size = 12),
plot.title = element_text(size = 16))animation_1 <- df_long %>%
filter(Type=="Global") %>%
filter(Indicator=="Deaths") %>%
filter(Year>=1980) %>%
filter(Year<=2010) %>%
ggplot(aes(x=Year, y=Value, group=Country, color=Region, size=Region,alpha=Region)) +
geom_point() +
scale_color_manual(values = c("Red","Black")) +
scale_alpha_manual(values = c(0.1, 1)) +
scale_size_manual(values = c(0.25, 2)) +
labs(title = "Distribution of Indicators by Year and Value, 1980-2010",
y = "Crude Mortality Rate") +
theme(axis.text.x = element_text(angle = 90)) +
theme_classic() +
transition_time(as.integer(Year)) +
shadow_wake(wake_length = 1, alpha = FALSE)
animate(animation_1, fps = 10, duration = 5)animation_2 <- df_long %>%
filter(Type=="Global") %>%
filter(Indicator=="Energy") %>%
filter(Year>=1980) %>%
filter(Year<=2010) %>%
ggplot(aes(x=Year, y=Value, group=Country, color=Region, size=Region, alpha=Region)) +
geom_point() +
scale_color_manual(values = c("Red","Black")) +
scale_alpha_manual(values = c(0.5, 1)) +
scale_size_manual(values = c(0.25, 2)) +
labs(title="Distribution of Indicators by Year and Value, 1980-2010",
y = "Energy Use per Capita") +
theme_classic() +
theme(axis.text.x = element_text(angle = 90)) +
transition_time(as.integer(Year)) +
shadow_wake(wake_length = 1, alpha = FALSE)
animate(animation_2, fps = 10, duration = 5)animation_3 <- df_long %>%
filter(Type=="Global") %>%
filter(Indicator=="GDP") %>%
filter(Year>=1980) %>%
filter(Year<=2010) %>%
ggplot(aes(x=Year, y=Value, group=Country, color=Region, size=Region, alpha=Region)) +
geom_point() +
scale_color_manual(values = c("Red","Black")) +
scale_alpha_manual(values = c(0.1, 1)) +
scale_size_manual(values = c(0.25, 2)) +
labs(title="Distribution of Indicators by Year and Value, 1980-2010",
y= "GDP Growth per Capita (%)") +
theme_classic() +
theme(axis.text.x = element_text(angle = 90)) +
transition_time(as.integer(Year)) +
shadow_wake(wake_length = 1, alpha = FALSE)
animate(animation_3, fps = 10, duration = 5)animation_4 <- df_long %>%
filter(Type=="Global") %>%
filter(Indicator=="Emissions") %>%
filter(Year>=1980) %>%
filter(Year<=2010) %>%
ggplot(aes(x=Year, y=Value, group=Country, color=Region, size=Region, alpha=Region)) +
geom_point() +
scale_color_manual(values = c("Red","Black")) +
scale_alpha_manual(values = c(0.1, 1)) +
scale_size_manual(values = c(0.25, 2)) +
labs(title = "Distribution of Indicators by Year and Value, 1980-2010",
y = "CO2 Emissions (Mg)") +
theme_classic() +
theme(axis.text.x = element_text(angle = 90)) +
transition_time(as.integer(Year)) +
shadow_wake(wake_length = 1, alpha = FALSE)
animate(animation_4, fps = 10, duration = 5)To color our plot we will use the viridis color pallette which is compatible with color-blindness by using the scale_fill_viridis_c() function which is simply available by loading the ggplot2 package.There are a few variations such as discreet as _d, or binned continuous as _b. The _c indicates a continuous scale. See here for more information.
Top10<-df_long %>%
filter(Type=="Global") %>%
filter(Indicator=="Emissions") %>%
filter(Year>=1900) %>%
group_by(Country) %>%
mutate(max_val = max(Value)) %>%
ungroup() %>%
mutate(rank=dense_rank(-max_val)) %>%
filter(rank<=10) %>%
ggplot(aes(x=Year, y=fct_reorder(Country, Value, max))) +
geom_tile(color="transparent", aes(fill=log(Value))) +
scale_fill_viridis_c()+
scale_x_continuous(breaks = seq(1900,2014,by=5),
labels = seq(1900,2014,by=5)) +
labs(title = "Top 10 "~CO[2]~"Emission-producing Countries in 2014",
subtitle = "Ordered by Emissions Produced in 2014",
fill = "Ln(CO2 Emissions (Mg))") +
theme_classic() +
theme(axis.text.x = element_text(size = 12, angle = 90),
axis.text.y = element_text(size = 12),
axis.title = element_blank(),
plot.caption = element_text(size = 12),
plot.title = element_text(size = 16),
legend.position = "bottom")
Top10 Interestingly, we can see that Germany had ver low imission rates at the end of World War II. We see that the US has consistently had high emission rates since 1900, but that the emission rates in China recently surpased that of the US.
Top10b<-df_long %>%
filter(Type=="Global") %>%
filter(Indicator=="Emissions") %>%
filter(Year>=1900) %>%
filter(Year<=2014) %>%
group_by(Country) %>%
mutate(max_val = max(Value)) %>%
ungroup() %>%
mutate(rank=dense_rank(-max_val)) %>%
filter(rank<=10) %>%
ggplot(aes(x=Year, y=Value, color = Country)) +
geom_line() +
scale_color_viridis_d()+
geom_text_repel(data = df_long %>%
filter(Type=="Global") %>%
filter(Indicator=="Emissions") %>%
filter(Year>=1900) %>%
filter(Year<=2014) %>%
group_by(Country) %>%
mutate(max_val = max(Value)) %>%
ungroup() %>%
mutate(rank=dense_rank(-max_val)) %>%
filter(rank<=10) %>%
filter(Year == last(Year)),
aes(label = Country,
x = Year,
y = Value),
size = 3,
alpha = 1,
nudge_x = 10,
direction = "y",
hjust = 1,
vjust = 1,
segment.size = 0.25,
segment.alpha = 0.25,
force = 1)+
theme_linedraw() +
theme(axis.text.x = element_text(angle = 90),
axis.title = element_blank(),
legend.position = "none") +
labs(title = "Top 10 Emissions-producing Countries in 2010 (1900-2014)",
subtitle = "Ordered by Emissions Produced in 2014",
fill = "Ln(CO2 Emissions (Mg))")
Top10b ## US-specific
df_long_us <- df_long %>%
filter(Country=="United States")
# Approximated derivative function)
df_long_us <- df_long_us %>%
filter(Year >= 1900,
Year <= 2010) %>%
group_by(Indicator) %>%
mutate("Change (%)"=((Value/lag(Value))*100)-100,
Mean=mean(Value),
Anomaly=Value-Mean) %>%
ungroup() %>%
mutate(Anomaly_color=ifelse(Anomaly>0,"Positive",
ifelse(Anomaly<0,"Negative","Zero")),
Anomaly_color=factor(Anomaly_color, levels = c("Positive",
"Negative",
"Zero"),
ordered = TRUE))US_Indicators <- df_long %>%
filter(Country=="United States")%>%
filter(Year>=1980) %>%
ggplot(aes(x=Year, y=Value)) +
geom_line() +
geom_smooth(method = "loess") +
facet_wrap(Label~., ncol=2, nrow=3, scales = "free_y") +
theme_linedraw() +
theme(axis.text.x = element_text(angle = 90, size = 12),
axis.text.y = element_text(size = 12),
strip.text.x = element_text(face = "bold", size = 12),
axis.title.y = element_blank(),
axis.title.x = element_text(size = 12)) +
labs(title = "US-specific Indicators")
US_Indicatorsdf_long_us %>%
filter(Year>=1980) %>%
filter(Year<=2014) %>%
ggplot(aes(x=Year, y=`Change (%)`, color=Indicator, fill="transparent")) +
geom_hline(yintercept=0.8, linetype=2) +
geom_hline(yintercept=1.2, linetype=2) +
geom_hline(yintercept = 1, linetype=3) +
geom_line(size=0.5) +
scale_x_continuous(breaks = seq(1980,2014,by=5),
labels = seq(1980,2014,by=5)) +
scale_y_continuous(breaks = seq(-500,1250, by=250),
labels = seq(-500,1250, by=250)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 90),
axis.title.x = element_blank(),
legend.position = "bottom",
legend.direction = "horizontal") +
labs(title = "US-specific Indicators (1980-2010)",
subtitle = "Change (%) Lines")df_long_us %>%
filter(Year>=1980) %>%
filter(Year<=2014) %>%
filter(Indicator=="Temperature"|
Indicator=="Energy"|
Indicator=="Emissions") %>%
ggplot(aes(x=Year, y=`Change (%)`, color=Indicator)) +
geom_hline(yintercept=0.8, linetype=2) +
geom_hline(yintercept=1.2, linetype=2) +
geom_hline(yintercept = 1, linetype=3) +
geom_line(size=1) +
scale_x_continuous(breaks = seq(1980,2014,by=5),
labels = seq(1980,2014,by=5)) +
scale_y_continuous(breaks = seq(-10,10, by=1),
labels = seq(-10,10, by=1),
limits = c(-10,10)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 90),
axis.title.x = element_blank(),
legend.position = "bottom",
legend.direction = "horizontal") +
labs(title = "Emissions, Energy Use, and Temperature (1980-2010)",
subtitle = "Change (%) Lines")df_long_us %>%
filter(Year>=1980) %>%
filter(Year<=2010) %>%
filter(Indicator=="Temperature"|
Indicator=="Energy"|
Indicator=="Emissions") %>%
ggplot(aes(x=Year, y=`Change (%)`, color=Indicator)) +
geom_hline(yintercept=0.8, linetype=2) +
geom_hline(yintercept=1.2, linetype=2) +
geom_hline(yintercept = 0, linetype=3) +
geom_smooth(size=1, alpha=0.1, aes(fill=Indicator), se=TRUE) +
scale_x_continuous(breaks = seq(1980,2010,by=5),
labels = seq(1980,2010,by=5)) +
scale_y_continuous(breaks = seq(-10,10, by=1),
labels = seq(-10,10, by=1),
limits = c(-10,10)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 90),
axis.title.x = element_blank(),
legend.position = "bottom",
legend.direction = "horizontal") +
labs(title = "US Emissions, Energy Use, and Temperatures (1980-2010)",
subtitle = "Smoothed Change (%) Lines")df_long_us %>%
filter(Year>=1980) %>%
filter(Year<=2010) %>%
filter(Indicator=="Emissions"|
Indicator=="Temperature") %>%
ggplot(aes(x=Year, y=Value)) +
geom_line() +
geom_smooth(method = "loess")+
scale_x_continuous(breaks = seq(1980,2010,by=5),
labels = seq(1980,2010,by=5)) +
facet_wrap(Indicator~., scales = "free_y", ncol=1) +
theme_classic() +
theme(axis.text.x = element_text(angle = 90),
axis.title = element_blank()) +
labs(title="US Emissions and Temperatures (1980-2010)")df_wide_US <-df_long_us %>%
select(-c(Label, Anomaly, Anomaly_color, Mean, "Change (%)", Type)) %>%
pivot_wider(names_from = "Indicator", values_from = Value)%>%
filter(Year>=1980) %>%
filter(Year<=2010)
cor.test(pull(df_wide_US, Emissions),
pull(df_wide_US, Temperature))
Pearson's product-moment correlation
data: pull(df_wide_US, Emissions) and pull(df_wide_US, Temperature)
t = 3.4815, df = 29, p-value = 0.0016
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.2334979 0.7524951
sample estimates:
cor
0.5429183
Pearson's product-moment correlation
data: pull(df_wide_US, Emissions) and pull(df_wide_US, Disasters)
t = 2.8569, df = 29, p-value = 0.007833
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.1370685 0.7057842
sample estimates:
cor
0.4686469
df_long_us %>%
filter(Indicator=="Emissions"|
Indicator=="Temperature") %>%
ggplot(aes(x=Year, y=Value)) +
geom_vline(xintercept = 1980, linetype=2, color="black") +
geom_vline(xintercept = 2010, linetype=2, color="black") +
geom_segment(aes(x=Year, y=Value, xend=Year, yend=Mean,color=Anomaly_color), size=1.25) +
scale_color_manual(values = c("red","blue","gray")) +
geom_hline(aes(yintercept=Mean), linetype=1, color="black") +
scale_x_continuous(breaks = seq(1900,2010,by=5),
labels = seq(1900,2010,by=5)) +
facet_wrap(Indicator~., scales = "free_y", ncol=1) +
theme_classic() +
theme(axis.text.x = element_text(angle = 90),
axis.title = element_blank(),
legend.position = "none") +
labs(title = "US Emissions and Temperatures (1900-2010)",
subtitle = "Indicator Mean Represented by Solid Black Line")df_long_us %>%
filter(Indicator == "Temperature"|
Indicator == "Emissions") %>%
ggplot(aes(x=Year, y=`Change (%)`)) +
annotate("rect", xmin=-Inf, xmax=Inf, ymin=0, ymax=Inf, alpha=0.25, fill="green") +
annotate("rect", xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=0, alpha=0.25, fill="red") +
geom_hline(yintercept=0, linetype=1) +
geom_segment(aes(x=Year, y=`Change (%)`, xend=Year, yend=0), size=1.25) +
facet_wrap(Indicator~., scales = "free_y", ncol=1) +
scale_x_continuous(breaks = seq(1900,2010,by=5),
labels = seq(1900,2010,by=5)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 90),
axis.title.x = element_blank(),
legend.position = "bottom",
legend.direction = "horizontal") +
labs(title = "US Emissions, Energy Use, and Temperatures (1980-2010)",
subtitle = "Change (%) Lines")library(patchwork)
CO2_world + Top10 + US_Indicators +
plot_layout(widths = c(1, 2), heights = unit(c(2, 5), c('cm', 'null')))png(here::here("img", "mainplot.png"), width = 900, height = 700)
(CO2_world | Top10)/ US_Indicators+
plot_layout(widths = c(1, 2), heights = unit(c(4, 5), c('cm', 'null')))
dev.off()quartz_off_screen
2